如果它不是'.',那麼該位置的字母已經是固定的了,剩下是'.'的位置就要由我們依照左到右,上到下的順序來分別填入該位置一個英文字母,而填入字母的方式是由'A'開始,如果其上下左右已經有'A',那麼再換'B',以此類推....,如果'A'能填就要填,舉個例子:
..B
.B.
...
的解答為
ACB
CBA
ACB
而不是底下這個
BAB
ABA
BAB
想法:
如果該點為'.',則從'A'開始,檢查其上下左右來決定'A'可否填入,不然就換'B','C'....,一直到可以填入為止
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
using namespace std; | |
char square[12][12]; | |
void Fill (int n,int i,int j){ | |
char filled = 'A'; | |
bool up,left,right,down; | |
while (filled <= 'Z'){ | |
if (i-1<0 || square[i-1][j]!=filled) up=1; else up=0; | |
if (i+1>=n || square[i+1][j]!=filled) down=1; else down=0; | |
if (j-1<0 || square[i][j-1]!=filled) left=1; else left=0; | |
if (j+1>=n || square[i][j+1]!=filled) right=1;else right=0; | |
if (up && down && left && right){ | |
square[i][j] = filled; | |
printf("%c",filled); | |
break; | |
} | |
else filled += 1; | |
} | |
} | |
int main() | |
{ | |
int t,n,Case=1; | |
scanf("%d",&t); | |
while (t--) | |
{ | |
scanf("%d",&n); | |
gets(square[0]); | |
for(int i=0;i<n;i++) gets(square[i]); | |
printf("Case %d:\n",Case++); | |
for(int i=0;i<n;i++){ | |
for(int j=0;j<n;j++){ | |
if (square[i][j]!='.') | |
printf("%c",square[i][j]); | |
else Fill(n,i,j); | |
} | |
printf("\n"); | |
} | |
} | |
return 0; | |
} |
沒有留言:
張貼留言