Search Problems,用Dfs。TLE,原来要剪枝。看了看网上高手的答案,下次再改吧。
http://blog.sina.com.cn/s/blog_70756f680100m0xo.html
http://hi.baidu.com/vivyli/blog/item/e53d78ea39c0f1d7d539c9f8.html
//ZOJ1008Gnome Tetravex
#include <iostream>
#include <memory.h>
#include <stdio.h>
//#define DEBUG
#define MAX_SIZE 5
#define MAX_SIZE_SQR (MAX_SIZE * MAX_SIZE)
using namespace std;
int Board[MAX_SIZE_SQR][4];
bool BdUsed[MAX_SIZE_SQR];
int Target[MAX_SIZE_SQR];
int TgPtr;
bool Dfs(int board_size)
{
#ifdef DEBUG
for (int i = 0; i < board_size * board_size; i ++)
{
cout << Target[i] << " ";
}
cout << "tgptr "<< TgPtr << endl;
#endif
int column, row;
bool is_same, is_psbl;
int check_ptr;
if (TgPtr >= board_size * board_size)
{
if (board_size <= 1)
return true;
if (Board[Target[board_size - 1]][2]
== Board[Target[board_size * board_size - 1]][0]
&& Board[Target[board_size * board_size - 2]][1]
== Board[Target[board_size * board_size - 1]][3])
{
return true;
}
return false;
}
for (int i = 0; i < board_size * board_size; i ++)
{
if (BdUsed[i] == true)
continue;
column = TgPtr / board_size;
row = TgPtr % board_size;
is_same = true;
if (column > 0) //need check top
{
check_ptr = Target[(column - 1) * board_size + row];
if (Board[check_ptr][2] == Board[i][0])
{
#ifdef DEBUG
cout << "(" << check_ptr << ", 2) "
<< Board[check_ptr][2] << " (" << i
<< ", 0) " << Board[i][0] << endl;
#endif
BdUsed[i] = true;
Target[TgPtr] = i;
TgPtr ++;
is_psbl = Dfs(board_size);
BdUsed[i] = false;
TgPtr --;
Target[TgPtr] = 0;
if (is_psbl == true)
return true;
}
}
if (row > 0) //need check left
{
check_ptr = Target[column * board_size + row - 1];
if (Board[check_ptr][1] == Board[i][3])
{
#ifdef DEBUG
cout << "(" << check_ptr << ", 1) "
<< Board[check_ptr][1] << " (" << i
<< ", 3) " << Board[i][3] << endl;
#endif
BdUsed[i] = true;
Target[TgPtr] = i;
TgPtr ++;
is_psbl = Dfs(board_size);
BdUsed[i] = false;
TgPtr --;
Target[TgPtr] = 0;
if (is_psbl == true)
return true;
}
}
#ifdef TEST
if (column < board_size) //need check bottom
{
check_ptr = Target[(column + 1) * board_size + row];
if (Board[check_ptr][0] == Board[i][2])
{
#ifdef DEBUG
cout << "(" << check_ptr << ", 0) "
<< Board[check_ptr][0] << " (" << i
<< ", 2) " << Board[i][2] << endl;
#endif
BdUsed[i] = true;
Target[TgPtr] = i;
TgPtr ++;
is_psbl = Dfs(board_size);
BdUsed[i] = false;
TgPtr --;
Target[TgPtr] = 0;
if (is_psbl == true)
return true;
}
}
if (row < board_size) //need check right
{
check_ptr = Target[column * board_size + row + 1];
if (Board[check_ptr][3] == Board[i][1])
{
#ifdef DEBUG
cout << "(" << check_ptr << ", 3) "
<< Board[check_ptr][3] << " (" << i
<< ", 1) " << Board[i][1] << endl;
#endif
BdUsed[i] = true;
Target[TgPtr] = i;
TgPtr ++;
is_psbl = Dfs(board_size);
BdUsed[i] = false;
TgPtr --;
Target[TgPtr] = 0;
if (is_psbl == true)
return true;
}
}
#endif
}
return false;
}
int main(int argc, char *argv[])
{
int board_size, game_cnt = 0;
bool is_psbl;
while (cin >> board_size && board_size != NULL)
{
for (int i = 0; i < board_size * board_size; i ++)
{
for (int j = 0; j < 4; j ++)
{
cin >> Board[i][j];
}
}
for (int i = 0; i < board_size * board_size; i ++)
{
memset(BdUsed, 0x0, sizeof(BdUsed));
BdUsed[i] = true;
Target[0] = i;
TgPtr = 1;
is_psbl = Dfs(board_size);
if (is_psbl == true)
{
break;
}
}
game_cnt ++;
cout << "Game " << game_cnt << ": ";
if (is_psbl == true)
{
cout << "P";
}
else
{
cout << "Imp";
}
cout << "ossible" << endl << endl;
}
return 0;
}