完美世界-方块消除游戏

/**
纯逻辑操作,仅抛砖引玉
测试通过88%,时间超时
**/
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class FangKuai {

	static int RED = 0, GREEN = 1, BLUE = 2, YELLOW = 3, PURPLE = 4, DEL = -1;
	static int[][] p = {
	        {RED,RED,BLUE,BLUE,GREEN,YELLOW,BLUE,YELLOW,RED,PURPLE},
	        {GREEN,GREEN,GREEN,BLUE,RED,PURPLE,RED,YELLOW,YELLOW,BLUE},
	        {BLUE,RED,RED,YELLOW,YELLOW,PURPLE,BLUE,GREEN,GREEN,BLUE},
	        {YELLOW,RED,BLUE,YELLOW,BLUE,RED,PURPLE,GREEN,GREEN,RED},
	        {YELLOW,RED,BLUE,BLUE,PURPLE,GREEN,PURPLE,RED,YELLOW,BLUE},
	        {PURPLE,YELLOW,RED,RED,YELLOW,RED,PURPLE,YELLOW,RED,RED},
	        {YELLOW,YELLOW,GREEN,PURPLE,GREEN,RED,BLUE,YELLOW,BLUE,GREEN},
	        {RED,YELLOW,BLUE,BLUE,YELLOW,GREEN,PURPLE,RED,BLUE,GREEN},
	        {GREEN,GREEN,YELLOW,YELLOW,RED,RED,PURPLE,BLUE,BLUE,GREEN},
	        {PURPLE,BLUE,RED,RED,PURPLE,YELLOW,BLUE,RED,RED,GREEN}};
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
			
		String[] arrStr = in.nextLine().trim().split(" ");
		int[] arrColor = {26, 18, 22, 21, 13};			// 颜色方块计数
		
		for (int i = 0; i < arrStr.length; i++) {
			int click = Integer.parseInt(arrStr[i]);
			xiaochu(p, click, arrColor);
		}
		
		for (int k = 0; k < arrColor.length; k++)		// 输出结果
			System.out.print(arrColor[k] + " ");	
	}
	
	public static void xiaochu(int[][] p, int clickNum, int[] arrColor) {
		if (p == null)
			return;
		
		Queue<Integer> queue = new LinkedList<Integer>();		// 队列存储需要环顾四周的节点位置
		queue.add(--clickNum);						// 当前点击点处理
		int r = clickNum / 10;
		int c = clickNum % 10;
		int color = p[r][c];
		p[r][c] = DEL;
		int count = 1;
		
		while (!queue.isEmpty()) {
			int click = queue.poll();		// 处理节点上下左右四个相邻节点
			int row = click / 10;
			int col = click % 10;
			
			if (row < 9 && p[row+1][col] == color) {
				queue.add((row+1)*10 + col);
				p[row+1][col] = DEL;
				count++;
			}
			if (row > 0 && p[row-1][col] == color) {
				queue.add((row-1)*10 + col);
				p[row-1][col] = DEL;
				count++;
			}
			if (col > 0 && p[row][col-1] == color) {
				queue.add(row*10 + col-1);
				p[row][col-1] = DEL;
				count++;
			}
			if (col < 9 && p[row][col+1] == color) {
				queue.add(row*10 + col+1);
				p[row][col+1] = DEL;
				count++;
			}
		}
		
		if (count > 1) {
			arrColor[color] -= count;				// 减少已清理颜色
			
			for (int i = 0; i < 10; i++) {				// 数组整理,已删除项上移  for (int j = 0; j < 10; j++) {
                	        if (p[j][i] == DEL) {
                		    int start = j-1;
                		    while (start >= 0 && p[start][i] != DEL) {
                			int tmp = p[start+1][i];
                			p[start+1][i] = p[start][i];
                			p[start][i] = tmp;
                			start--;    }
                	        }
                            } }
			
			int[] arrTmp = new int[10];					// 验证是否有某列全清理了
			for (int i = 0; i < 10; i++) {
                            for (int j = 0; j < 10; j++) {
                	        arrTmp[i] += p[j][i];
                            }
			}
			for (int k = arrTmp.length-2; k >= 0; k--) {
				if (arrTmp[k] == -10) {					// 整列往左移
					for (int i = k+1; i < 10; i++) {
		                            for (int j = 0; j < 10; j++) {
		                	        int tmp = p[j][i-1];
                			        p[j][i-1] = p[j][i];
                			        p[j][i] = tmp;    }
					}
				}
			}
		}
		else
			p[r][c] = color;
	}
}


#完美世界##Java工程师#
全部评论
666,我磨第二道题一直卡在75,第一道没时间看。
点赞
送花
回复
分享
发布于 2016-09-19 22:43
两处小错误,考试完才发现。。。下面是修改好的,应该是对的吧༼ ・ ౪ ・ ༽ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int RED = 0, GREEN = 1, BLUE = 2, YELLOW = 3, PURPLE = 4; int[][] p = { {RED,RED,BLUE,BLUE,GREEN,YELLOW,BLUE,YELLOW,RED,PURPLE}, {GREEN,GREEN,GREEN,BLUE,RED,PURPLE,RED,YELLOW,YELLOW,BLUE}, {BLUE,RED,RED,YELLOW,YELLOW,PURPLE,BLUE,GREEN,GREEN,BLUE}, {YELLOW,RED,BLUE,YELLOW,BLUE,RED,PURPLE,GREEN,GREEN,RED}, {YELLOW,RED,BLUE,BLUE,PURPLE,GREEN,PURPLE,RED,YELLOW,BLUE}, {PURPLE,YELLOW,RED,RED,YELLOW,RED,PURPLE,YELLOW,RED,RED}, {YELLOW,YELLOW,GREEN,PURPLE,GREEN,RED,BLUE,YELLOW,BLUE,GREEN}, {RED,YELLOW,BLUE,BLUE,YELLOW,GREEN,PURPLE,RED,BLUE,GREEN}, {GREEN,GREEN,YELLOW,YELLOW,RED,RED,PURPLE,BLUE,BLUE,GREEN}, {PURPLE,BLUE,RED,RED,PURPLE,YELLOW,BLUE,RED,RED,GREEN}}; String[] strs = sc.nextLine().split(" "); int[] poss = new int[strs.length]; for (int i=0; i<poss.length; i++) poss[i] = Integer.valueOf(strs[i]); for (int i=0; i<poss.length; i++) { int m = (poss[i] - 1) / 10; int n = (poss[i] - 1) % 10; int num = p[m][n]; // 如果当前位置不为空,则递归消除之 if (num == 5) continue; solve(p, m, n, num); // 如果下方为空,则向下移动 for (int y=0; y<10; y++) { int bottom = 9; int nonempty = 9; while (true) { while (nonempty >= 0 && p[nonempty][y] == 5) nonempty--; if (nonempty < 0) break; if (bottom != nonempty) { p[bottom][y] = p[nonempty][y]; p[nonempty][y] = 5; } bottom--; nonempty--; } } // 如果左侧整列为空,则向左移动 int left = 0; int nonemptyline = 0; while (true) { while (nonemptyline < 10) { boolean flag = true; for (int x=0; x<10; x++) { if (p[x][nonemptyline] != 5) { flag = false; break; } } if (flag) nonemptyline++; else break; } if (nonemptyline >= 10) break; if (left != nonemptyline) { for (int x=0; x<10; x++) { p[x][left] = p[x][nonemptyline]; p[x][nonemptyline] = 5; } } left++; nonemptyline++; } } // 统计分类方块的数量(包含有颜色的方块和空方块) int[] res = new int[6]; for (int x=0; x<10; x++) for (int y=0; y<10; y++) res[p[x][y]]++; for (int i=0; i<5; i++) { System.out.print(res[i]); if (i != 4) System.out.print(" "); else System.out.println(); } } } private static void solve(int[][] p, int m, int n, int num) { int[][] d = {{-1,0}, {1,0}, {0,-1}, {0,1}}; for (int i=0; i<4; i++) { int newm = m + d[i][0]; int newn = n + d[i][1]; if (newm < 0 || newm >= 10 || newn < 0 || newn >= 10) continue; if (p[newm][newn] == num) { p[newm][newn] = 5; solve(p, newm, newn, num); } } } }
点赞
送花
回复
分享
发布于 2016-09-19 23:13
秋招专场
校招火热招聘中
官网直投

相关推荐

永联 dsp工程师 15k*15 双非硕士
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务