您的位置:首页 > Web前端

uva10422 - Knights in FEN

2013-12-23 15:06 399 查看
利用set 进行判断重复, 可以用dfs 因为只有11步。 我这里用了bfs

题目:

Problem D

Knights in FEN

Input: standard input

Output: standard output

Time Limit: 10 seconds

There are black and white knights on a 5 by 5 chessboard. There are twelve of each color, and there is one square that is empty. At any time, a knight can move into an empty square as long as it moves like a knight in normal chess (what else did you expect?).

Given an initial position of the board, the question is: what is the minimum number of moves in which we can reach the final position which is:

Input

First line of the input file contains an integer N (N<14) that indicates how many sets of inputs are there. The description of each set is given below:

Each set consists of five lines; each line represents one row of a chessboard. The positions occupied by white knights are marked by 0 and the positions occupied by black knights are marked by 1. The space corresponds to the empty square on board.

There is no blank line between the two sets of input.

The first set of the sample input below corresponds to this configuration:

Output

For each set your task is to find the minimum number of moves leading from the starting input configuration to the final one. If that number is bigger than 10, then output one line stating

Unsolvable in less than 11 move(s).


otherwise output one line stating

Solvable in n move(s).

where n <= 10.

The output for each set is produced in a single line as shown in the sample output.

Sample Input

2

01011

110 1

01110

01010

00100

10110

01 11

10111

01001

00000

Sample Output

Unsolvable in less than 11 move(s).

Solvable in 7 move(s).

代码:


#include <iostream>
#include <set>
#include <algorithm>
#include <memory.h>
#include <cstdio>
using namespace std;

typedef int State[25];
const  int maxn = 5000000;
State st [maxn];
State goal = {1,1,1,1,1,
0,1,1,1,1,
0,0,2,1,1,
0,0,0,0,1,
0,0,0,0,0};
int dis[maxn];
int dx[]={-1,-2,-1,-2,1,2,1,2};
int dy[]={-2,-1,2,1,-2,-1,2,1};

struct cmp
{
bool operator ()(int a,int b)const
{
return memcmp(&st[a],&st[b],sizeof(st[b]))<0;
}

};

set<int,cmp> vis;
int try_to_insert(int s)
{
if(vis.count(s))return 0;
vis.insert(s);
return 1;
}

int bfs()
{
vis.clear();

int front=0,rear=1;

while(front<rear)
{
State& s = st[front];
if(memcmp(goal,s,sizeof(s))==0) return front;

int z;
for( z=0;z<25;z++)if(s[z]==2)break;

int x = z/5,y=z%5;

for(int d=0;d<8;d++)
{
int newx = x+dx[d];
int newy = y+dy[d];
int newz = newx*5+newy;

if(newx>=0&&newx<5 && newy>=0&&newy<5)
{
//                cout<<"Old "<<x<<" "<<y<<" "<<z<<endl;
//
//               cout<<"New "<<newx<<" "<<newy<<" "<<newz<<endl;
State & t = st[rear];
memcpy(&t,&s,sizeof(s));

t[newz]=s[z];
t[z]=s[newz];

dis[rear]=dis[front]+1;

if(try_to_insert(rear))
{
//                    for(int i=0;i<5;i++)
//                    {
//                        for(int j=0;j<5;j++)
//                        {
//                            cout<<st[rear][i*5+j];
//                        }
//                        cout<<endl;
//                    }
rear++;
}
}
}
if(dis[front]>=11)return -1;
front++;

}
return -1;

}

int main()
{
int tst;
cin>>tst;
cin.ignore();
while(tst--)
{
char c;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{

c=getchar();
if(c==' ')st[0][i*5+j] = 2;
else
st[0][i*5+j]  = c-'0';
}
getchar();
}

int ans;
ans = bfs();
if(ans==-1)cout<<"Unsolvable in less than 11 move(s)."<<endl;
else
cout<<"Solvable in "<<dis[ans]<<" move(s)."<<endl;

//memset(dis,0,sizeof(dis));
}

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