您的位置:首页 > 其它

UVA 639 Don't Get Rooked

2012-10-11 13:16 309 查看
简单回溯

CODE:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;

const int SIZE = 6;
int MAX;
int maze[SIZE][SIZE];
int N;

int check(int r, int c)
{
int i;
for(i = r-1; i >= 0; i--)
{
if(maze[i][c] == 'Y') return 0;
if(maze[i][c] == 'X') break;
}
for(i = c-1; i >= 0; i--)
{
if(maze[r][i] == 'Y') return 0;
if(maze[r][i] == 'X') break;
}
return 1;
}

void dfs(int i, int s)
{
if(i == N*N)
{
MAX = max(MAX, s);
return ;
}

int r = i/N, c = i%N; //行,列。

if(maze[r][c] == '.' && check(r, c))
{
maze[r][c] = 'Y';
dfs(i+1, s+1);
maze[r][c] = '.';
}
dfs(i+1, s);
}

int main()
{
int i, j;
while(~scanf("%d", &N), N)
{
getchar();
memset(maze, 0, sizeof(maze));
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
scanf("%c", &maze[i][j]);
}
getchar();
}
MAX = -1;
dfs(0, 0);
printf("%d\n", MAX);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: