题目链接:点击这里。
思路
本题思路比较简单,由于要求字典序最小,因此从第一个位置开始,每次从A开始试错,如果可以满足,则进行下一位。
代码
我使用了int
类型进行存储,事实证明比标准AC麻烦了。虽然很多时候字符串要转换为int
类型便于处理,但这里不需要,直接把每行当成一个字符串就够了。
类似于AC标准答案,我们可以定义一个template
类型,使得任意类型都可进行类似字典比较。
个人代码:
#include <iostream>
#include <string.h>
using namespace std;
int N;
int alphabets[11][11];
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1}; /* up down left right */
/* return 0 means OK, else indicats the dirction */
int check(int row, int col)
{
for (int i = 0; i < 4; i++)
{
int n_row = dir[i][0] + row;
int n_col = dir[i][1] + col;
// cout << "x: " << row << " y: " << col << endl;
// cout << "new x: " << n_row << " new y: " << n_col << endl;
/* if bounded */
if (n_row >= 1 && n_row <= N && n_col >= 1 && n_col <= N)
{
if (alphabets[n_row][n_col] == alphabets[row][col])
return i + 1;
}
}
return 0;
}
void init()
{
for (int i = 0; i <= N; i++)
{
for (int j = 0; j <= N; j++)
alphabets[i][j] = -1;
}
return;
}
void read_data()
{
cin >> N;
init();
char temp;
int i = 1, j = 1;
while (i <= N)
{
if (j > N)
{
j = 1;
i++;
}
else
{
cin >> temp;
if (temp != '.')
alphabets[i][j] = (int)(temp - 'A');
j++;
}
}
}
void print()
{
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
char temp = 'A' + alphabets[i][j];
cout << temp;
}
cout << endl;
}
}
int procss(int row, int col)
{
int flag = 0;
if (row > N)
return 1;
/* if the char has been setted */
if (alphabets[row][col] != -1)
flag = 1;
/* find the least alphabets */
for (int i = 0; i < 26; i++)
{
if (flag)
break;
alphabets[row][col] = i;
if (check(row, col) == 0)
break;
}
/* look for next position */
if (col == N)
procss(row + 1, 1);
else
procss(row, col + 1);
}
int main()
{
int kcase, number = 1;
cin >> kcase;
while (kcase--)
{
read_data();
procss(1, 1);
cout << "Case " << number++ << ":" << endl;
print();
}
return 0;
}
欢迎关注我的个人博客。