ZOJ - 1008
Description 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.
It seems the game is not so hard. But indeed, Hart is not accomplished in the game. He can finish the easiest game successfully. When facing with a more complex game, he can find no way out.
Game 2: Impossible Source
Asia 2001, Shanghai (Mainland China)
|
这道题的时间限制是10s,看起来真是非常nuiblity,然后动手做了一下,一开始WA,后来TLE,然后TLE,然后还是TLE......。隔了几天终于忍不住看题解,说是需要将相同的方块放在一起,文中还慢悠悠地补了一句,说是TAOCP中有提到这种优化,看来以后还是要多看书啊。
在漫长的TLE岁月里,积累了一些优化的方法,我相信他们是有用的,比如讲方块按照四个方向上的数字归类好。这样查找的时候效率会比较高。
后来用map重新写了一下,增进了我对map的了解。
在使用map的时候,你只需要重载一个<操作符,可想而知,其实所有的判等,大于,小于都是依靠这个操作符实现的。
所以当我把方块的结构体写成下面这个样子的时候,代码果断WA了。
struct grid{
int up,right,down,left;
void Read(){
scanf("%d%d%d%d",&up,&right,&down,&left);
}
bool operator < (const grid& cmper)const{
return up>cmper.up;
}
} g[size*size];
此时题目考虑两个元素是否相同只看他的up。
所以当我把方块的结构体写成下面这个样子的时候,代码果断TLE了。
struct grid{
int up,right,down,left;
void Read(){
scanf("%d%d%d%d",&up,&right,&down,&left);
}
bool operator < (const grid& cmper)const{
if (up!=cmper.up)
return up < cmper.up;
if (right!=cmper.right)
return right < cmper.right;
if (down!=cmper.down)
return down < cmper.down;
if (left!=cmper.left)
return left < cmper.left;
}
} g[size*size];
此时一次比较最多需要比较四次。
我甚至一度想过散列,因为在这道题目中,次序对我毫无用处。最后果然散列了。
struct grid{
int up,right,down,left;
int sum;
void Read(){
scanf("%d%d%d%d",&up,&right,&down,&left);
sum = ((up*10 + right)*10 + down)*10 + left;
}
bool operator < (const grid& cmper)const{
return sum < cmper.sum;
}
} g[size*size];
想想我也是蠢,其实一个gird也许只用一个数字就能存储了。比较的时候比较对应位上的数字就好了,不过现在懒得修改了。
最后是AC代码,其实除了那几个坑,这就是一道很简单的题目。
# include <cstdio>
# include <map>
using namespace std;
const int size = 5 + 3;
const int debug = 0;
struct grid{
int up,right,down,left;
int sum;
void Read(){
scanf("%d%d%d%d",&up,&right,&down,&left);
sum = ((up*10 + right)*10 + down)*10 + left;
}
bool operator < (const grid& cmper)const{
return sum < cmper.sum;
}
} g[size*size];
int n;
map<grid,int> mp;
bool match(int no){
int up = no-n,left = no-1;
bool ret = true;
if (up>0)
ret = ret&&g[no].up==g[up].down;
if (left%n!=0)
ret = ret&&g[no].left==g[left].right;
return ret;
}
bool dfs(int no){
if (no==n*n+1)
return true;
map<grid,int>::iterator it;
for (it=mp.begin();it!=mp.end();it++)
{
if (it->second<=0)
continue;
g[no] = it->first;
if (match(no)){
it->second--;
if (dfs(no+1))
return true;
it->second++;
}
}
return false;
}
int main()
{
int i,ncase = 0;
while(scanf("%d",&n)&&n){
mp.clear();
if (ncase++) printf("\n");
for (i=0;i<n*n;i++)
{
grid tmp;
tmp.Read();
mp[tmp]++;
}
if (dfs(1))
printf("Game %d: Possible\n",ncase);
else
printf("Game %d: Impossible\n",ncase);
}
return 0;
}
速度也只是比超时快3秒,有时间再优化。