URL: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008
Time Limit: 10 Seconds Memory Limit: 32768 KB
Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n*n squares. Each square is divided into four triangles marked four numbers (range from 0 to 9). In a square, the triangles are the left triangle, the top triangle, the right triangle and the bottom triangle. For example, Fig. 1 shows the initial state of 2*2 squares.
The player is required to move the squares to the termination state. In the termination state, any two adjoining squares should make the adjacent triangle marked with the same number. Fig. 2 shows one of the termination states of the above example.
Game 2: Impossible
简单的DFS问题,代码里唯一的剪枝优化是将相同的正方形记录在同一个数组元素里,这样在搜索过程中可避免对相同正方形的反复检测。
#include <cstdio>
#include <cstring>
struct square{
int u, r, l, d;
int num;
}s[25];
bool flag;
int n, nn, cnt;
int step[26];
bool ok(int index, int pos){
if(pos / n == 0) {if( s[ step[pos-1] ].r == s[index].l ) return true;}
else if(pos % n == 0) {if( s[ step[pos-n] ].d == s[index].u ) return true;}
else if(s[step[pos-1]].r == s[index].l && s[step[pos - n]].d == s[index].u) return true;
return false;
}
void DFS(int index, int pos){
if(pos == nn - 1) {flag = true; return;}
step[pos] = index;
for(int i = 0; i < cnt; i++)
if(s[i].num > 0)
if(ok(i, pos + 1)){
s[i].num--;
DFS(i, pos + 1);
if(flag) return;
s[i].num++;
}
}
int main(){
int u, r, l, d;
int i, j, g = 0;
while(~scanf("%d", &n) && n){
nn = n * n; cnt = 0;
std::memset(s, 0, sizeof(s));
for(i = 0; i < nn; i++){
scanf("%d %d %d %d", &u, &r, &d, &l);
for(j = 0; j < cnt; j++)
if(s[j].u == u && s[j].r == r && s[j].d == d && s[j].l == l){
s[j].num++; break;
}
if(j == cnt){
s[cnt].u = u; s[cnt].r = r; s[cnt].d = d; s[cnt].l = l;
s[cnt].num = 1; cnt += 1;
}
}
for(i = 0; i < cnt; i++){
flag = false;
s[i].num--;
DFS(i, 0);
if(flag){
if(++g != 1) printf("\n");
printf("Game %d: Possible\n", g); break;
}
s[i].num++;
}
if(i == cnt){
if(++g != 1) printf("\n");
printf("Game %d: Impossible\n", g);
}
}
return 0;
}