您的位置:首页 > 编程语言 > C语言/C++

2012蓝桥杯本科组C/C++决赛题

2013-11-10 12:15 381 查看
星期几

#include <iostream>
#include <cstring>
using namespace std;

const int N = 6;
const char mark[N + 1] = "ABCDEF";
int n = 1;
char map

, s

;

bool input(void)
{
int n;
char x, y, c;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
cin >> map[i][j];
}
}
(cin >> n).get();
while(n--)
{
cin.get(x).get(y).get(c).get();
s[x-48][y-48] = c;
}
return !cin.eof();
}

void clean(void)
{
n = 1;
memset(s, 0, sizeof(int) * N * N);
memset(map, 0, sizeof(int) * N * N);
}

void display(void)
{
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
cout << s[i][j] << " ";
}
cout << endl;
}
}

bool isOK(int x, int y, char ch)
{
for(int i = 0; i < N; i++)
{
if(s[i][y] == ch || s[x][i] == ch)
{
return false;
}
for(int j = 0; j < N; j++)
{
if(map[i][j] == map[x][y])
{
if(s[i][j] == ch)
{
return false;
}
}
}
}
return true;
}

void DFS(int x, int y)
{
if(x == N && y == 0)
{
cout << n++ << endl;
display();
return;
}
if(s[x][y] == 0)
{
for(int i = 0; i < N ; i++)
{
if(isOK(x, y, mark[i]))
{
s[x][y] = mark[i];
if(y == N - 1)
{
DFS(x + 1, 0);
}
else
{
DFS(x, y + 1);
}
s[x][y] = 0;
}
}
}
else
{
if(y == N - 1)
{
DFS(x + 1, 0);
}
else
{
DFS(x, y + 1);
}
}
}

int main()
{
while(input())
{
DFS(0, 0);
clean();
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: