网易游戏黑白棋题解

import java.util.Scanner;


public class 黑白棋 {
  public static void main(String[] args) {
	   Scanner in=new Scanner(System.in);
	   while(in.hasNext()){
		   int n=in.nextInt();
		   for (int i = 0; i < n; i++) {
			char[][] arr=new char[8][8];
			for (int j = 0; j < arr.length; j++) {
				String s=in.next();
				for (int k = 0; k < arr.length; k++) {
					arr[j][k]=s.charAt(k);
				}
			}
			int k=in.nextInt();
			int[][] bu=new int[k][3];
			for (int j = 0; j < bu.length; j++) {
				for (int j2 = 0; j2 < bu[0].length; j2++) {
					bu[j][j2]=in.nextInt();
				}
			}
			cal(arr,bu);
			int a=0;
			int b=0;
			for (int j = 0; j < arr.length; j++) {
				for (int j2 = 0; j2 < arr[0].length; j2++) {
					if(arr[j][j2]=='*') a++;
					else if(arr[j][j2]=='o') b++;
				}
			}
			System.out.println(a+" "+b);
		}
	   }

}
/**
 * 
 * ........
   ........
   ....*o..
   ...**o..
   ...ooo..
   ........
   ........
   ........
 * @param arr
 * @param bu
 * @return
 */
  
  
private static void cal(char[][] arr, int[][] bu) {
    for (int i = 0; i < bu.length; i++) {
		int x=bu[i][0];
		int y=bu[i][1];
		int type=bu[i][2];
		char t=type==0?'*':'o';
		//8个方向
		findAndMod(arr,x,y,t);
	}	
}

private static void findAndMod(char[][] arr,int x, int y, char type) {
	//8个方向
	//往右
	right(arr,x,  y, type);
	//往左
	left(arr,x,  y, type);
	//上
	shang(arr,x,  y, type);
	//下
	xia(arr,x,  y, type);
	//左上
	zuoshang(arr,x,  y, type);
	//右下
	youxia(arr,x,  y, type);
//	左下
	zuoxia(arr,x,  y, type);
	//右上
	youshang(arr,x,  y, type);
}

private static void youshang(char[][] arr, int x, int y, char type) {
	int flag=0;
	int index_x=0;
	int index_y=0;
	for (int i = x-1,j=y+1; i>=0&&j<=7; i--,j++) {
		if(arr[i][j]==type) {
			flag=1;
			index_x=i;
			index_y=j;
			break;
		}
		if(arr[i][j]=='.'){
			break;
		}
	}
	if(flag==1){
	for (int i = x,j=y; i>=index_x&&j<=index_y; i--,j++) {
		arr[i][j]=type;
	}
	}


}

private static void zuoxia(char[][] arr, int x, int y, char type) {

	int flag=0;
	int index_x=0;
	int index_y=0;
	for (int i = x+1,j=y-1; i<=7&&j>=0; i++,j--) {
		if(arr[i][j]==type) {
			flag=1;
			index_x=i;
			index_y=j;
			break;
		}
		if(arr[i][j]=='.'){
			break;
		}
	}
	if(flag==1){
	for (int i = x,j=y; i<=index_x&&j>=index_y; i++,j--) {
		arr[i][j]=type;
	}
	}

}

private static void youxia(char[][] arr, int x, int y, char type) {
	int flag=0;
	int index_x=0;
	int index_y=0;
	for (int i = x+1,j=y+1; i<=7&&j<=7; i++,j++) {
		if(arr[i][j]==type) {
			flag=1;
			index_x=i;
			index_y=j;
			break;
		}
		if(arr[i][j]=='.'){
			break;
		}
	}
	if(flag==1){
	for (int i = x,j=y; i<=index_x&&j<=index_y; i++,j++) {
		arr[i][j]=type;
	}
	}

	
}

private static void zuoshang(char[][] arr, int x, int y, char type) {
	int flag=0;
	int index_x=0;
	int index_y=0;
	for (int i = x-1,j=y-1; i>=0&&j>=0; i--,j--) {
		if(arr[i][j]==type) {
			flag=1;
			index_x=i;
			index_y=j;
			break;
		}
		if(arr[i][j]=='.'){
			break;
		}
	}
	if(flag==1){
	for (int i = x,j=y; i>=index_x&&j>=index_y; i--,j--) {
		arr[i][j]=type;
	}
	}

}

private static void xia(char[][] arr, int x, int y, char type) {
	int flag=0;
	int index=0;
	for (int i = x+1; i<=7; i++) {
		if(arr[i][y]==type) {
			flag=1;
			index=i;
			break;
		}
		if(arr[i][y]=='.'){
			break;
		}
	}
	if(flag==1){
	for (int i = x; i<=index; i++) {
		arr[i][y]=type;
	}
	}
}

private static void shang(char[][] arr, int x, int y, char type) {
	int flag=0;
	int index=0;
	for (int i = x-1; i>=0; i--) {
		if(arr[i][y]==type) {
			flag=1;
			index=i;
			break;
		}
		if(arr[i][y]=='.'){
			break;
		}
	}
	if(flag==1){
	for (int i = x; i>=index; i--) {
		arr[i][y]=type;
	}
	}
}

private static void left(char[][] arr, int x, int y, char type) {
	int flag=0;
	int index=0;
	for (int i = y-1; i>=0; i--) {
		if(arr[x][i]==type) {
			flag=1;
			index=i;
			break;
		}
		if(arr[x][i]=='.'){
			break;
		}
	}
	if(flag==1){
	for (int i = y; i>=index; i--) {
		arr[x][i]=type;
	}
	}

}

private static void right(char[][] arr, int x, int y, char type) {
	int flag=0;
	int index=0;
	for (int i = y+1; i<=7; i++) {
		if(arr[x][i]==type) {
			flag=1;
			index=i;
			break;
		}
		if(arr[x][i]=='.'){
			break;
		}
	}
	if(flag==1){
	for (int i = y; i <=index; i++) {
		arr[x][i]=type;
	}
	}
}
}


全部评论
卧槽 代码量这么大 还好我没参加
点赞 回复 分享
发布于 2016-09-18 22:27
方法比较笨,见谅。。
点赞 回复 分享
发布于 2016-09-18 22:21
感觉思路一样啊。。但是不知道我哪里错了,麻烦指导下 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include<algorithm> #include<vector> #include<map> #include<queue> #include<cmath> using namespace std; char a[10][10]; int w[4][2]; void solve(int x,int y,char tmp){ for(int m = 0;m<8;m++){ int zz=0;int flag =true; int i=w[m][0]; int j=w[m][1]; for(;(y+j)<8&&(x+i)<8&&(y+j)>-1&&(x+i)>-1;i+=w[m][0],j+=w[m][1]){ if(a[x+i][y+j]=='.'){ flag =false; break; }else if(a[x+i][y+j] == tmp){ break; } zz++; } i=0;j=0; if(flag && zz >= 1){ for(int n = 0;n<zz;n++){ i+=w[m][0];j+=w[m][1]; a[x+i][y+j] = tmp; //solve(x+i,y+j,tmp); } } } return; } int cal(char x){ int sum = 0; for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ if(a[i][j] == x){ sum++; } } } return sum; } int main() { // freopen("s.txt","r",stdin); int t; while(cin>>t){ w[0][0] = 1;w[0][1] = 1; w[1][0] = -1;w[1][1] = -1; w[2][0] = 1;w[2][1] = -1; w[3][0] = -1;w[3][1] = 1; w[4][0] = 0;w[4][1] = 1; w[5][0] = 0;w[5][1] = -1; w[6][0] = -1;w[6][1] = 0; w[7][0] = 1;w[7][1] = 0; while(t--){ for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ cin>>a[i][j]; } } int n,x,y,z; cin>>n; for(int i=0;i<n;i++){ cin>>x>>y>>z; char tmp; if(z == 0) tmp = '*'; if(z==1) tmp = 'o'; a[x][y] = tmp; solve(x,y,tmp); } int hei = cal('*'); int bai = cal('o'); cout<<hei<<" "<<bai<<endl; } } return 0; }
点赞 回复 分享
发布于 2016-09-18 22:32
。。。我会告诉你黑白棋我完整自己实现过吗,然而特么我并没有这题
点赞 回复 分享
发布于 2016-09-18 22:58

相关推荐

不愿透露姓名的神秘牛友
11-27 10:48
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务