Diamond mine is a mini-game which is played on an 8 * 8 board as you can see below.
The board is filled with different colors of diamonds. The player can make one move at a time. A move is legal if it swaps two adjacent diamonds(not diagonally) and after that, there are three or more adjacent diamonds in a row or column with the same color. Those diamonds will be taken away and new diamonds will be put in their positions. The game continues until no legal moves exist.
Given the board description. You are going to determine whether a move is legal.
Input
The input contains several cases. Each case has exactly 9 lines. The first 8 lines each contains a string of 8 characters. The characters are 'R'(Red), 'O'(Orange), 'G'(Green), 'P'(Purple), 'W'(White), 'Y'(Yellow) or 'B'(Blue). All characters are uppercase. No 3 diamonds of the same color are initially in adjacent positions in a row or column. The last line has 4 integers in the form " row1 column1 row2 column2" describing the postions of the 2 diamonds that the player wants to swap. Rows are marked 1 to 8 increasingly from top to bottom while columns from left to right. Input is terminated by EOF.
Output
For each case, output "Ok!" if the move is legal or "Illegal move!" if it is not.
Sample Input
PBPOWBGW
RRPRYWWP
YGBYYGPP
OWYGGRWB
GBBGBGGR
GBWPPORG
PPGORWOG
WYWGYWBY
4 3 3 3
PBPOWBGW
RRPRYWWP
YGBYYGPP
OWYGGRWB
GBBGBGGR
GBWPPORG
PPGORWOG
WYWGYWBY
5 5 6 5
Sample Output
Ok!
Illegal move!
//做这个题,主要是细节问题,要把所有情况都考虑到
//虽然代码比较长,但我感觉好理解点。
#include<stdio.h>
#include<string.h>
#include<math.h>
#define abs(a) (a>=0?a:-a)
char a[10][10];
int main(){
int x1,x2,y1,y2;
int i=0;
char c;
while(scanf("%s",a[++i]+1)!=EOF)
{
if(i==8)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if((x1==x2&&abs(y1-y2)>1)||(y1==y2&&abs(x1-x2)>1)||x1==x2&&y1==y2)
{
printf("Illegal move!\n");
i=0;
continue;
}
else if(x1==x2||y1==y2)
{
c=a[x1][y1];
a[x1][y1]=a[x2][y2];
a[x2][y2]=c;
//这块判断坐标(x1,y1)横向是否有3个相连的
if((a[x1][y1]==a[x1][y1-1]&&a[x1][y1]==a[x1][y1-2])||(a[x1][y1]==a[x1][y1+1]&&a[x1][y1]==a[x1][y1+2])||(a[x1][y1]==a[x1][y1+1]&&a[x1][y1]==a[x1][y1-1]))
{
printf("Ok!\n");
i=0;
continue;
}
//这块判断坐标 (x1,y1)纵向是否有三个相连
else if((a[x1][y1]==a[x1-1][y1]&&a[x1][y1]==a[x1-2][y1])||(a[x1][y1]==a[x1+1][y1]&&a[x1][y1]==a[x1+2][y1])||(a[x1][y1]==a[x1+1][y1]&&a[x1][y1]==a[x1-1][y1]))
{
printf("Ok!\n");
i=0;
continue;
}
//这块判断坐标 (x2,y2)横向是否有三个相连
else if((a[x2][y2]==a[x2][y2-1]&&a[x2][y2]==a[x2][y2-2])||(a[x2][y2]==a[x2][y2+1]&&a[x2][y2]==a[x2][y2+2])||(a[x2][y2]==a[x2][y2-1]&&a[x2][y2]==a[x2][y2+1]))
{
printf("Ok!\n");
i=0;
continue;
}
//这块判断坐标 (x2,y2)纵向是否有三个相连
else if((a[x2][y2]==a[x2-1][y2]&&a[x2][y2]==a[x2-2][y2])||(a[x2][y2]==a[x2+1][y2]&&a[x2][y2]==a[x2+2][y2])||(a[x2][y2]==a[x2-1][y2]&&a[x2][y2]==a[x2+1][y2]))
{
printf("Ok!\n");
i=0;
continue;
}
else
{
printf("Illegal move!\n");
i=0;
continue;
}
}
else
printf("Illegal move!\n");
i=0;
}
}
return 0;
}