您的位置:首页 > 大数据 > 人工智能

HDOJ 题目1401 Solitaire(双向BFS)

2015-10-28 21:48 351 查看

Solitaire

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3850 Accepted Submission(s): 1165



[align=left]Problem Description[/align]
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.

There are four identical pieces on the board. In one move it is allowed to:

> move a piece to an empty neighboring field (up, down, left or right),

> jump over one neighboring piece to an empty field (up, down, left or right).



There are 4 moves allowed for each piece in the configuration shown above. As an example let's consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right.

Write a program that:

> reads two chessboard configurations from the standard input,

> verifies whether the second one is reachable from the first one in at most 8 moves,

> writes the result to the standard output.

[align=left]Input[/align]
Each of two input lines contains 8 integers a1, a2, ..., a8 separated by single spaces and describes one configuration of pieces on the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece - the row
number and the column number respectively. Process to the end of file.

[align=left]Output[/align]
The output should contain one word for each test case - YES if a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word NO otherwise.

[align=left]Sample Input[/align]

4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6


[align=left]Sample Output[/align]

YES


[align=left]Source[/align]
Southwestern Europe 2002

[align=left]Recommend[/align]
Ignatius.L | We have carefully selected several similar problems for you: 1240 1180 1043 1044 1515
题目大意:就是给了初始4个棋子的坐标,又给了目标4个旗子的坐标,可以通过走上下左右4个方向,或隔着自己的一个子跳一步,问能不能再8步之内完成
ac代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
char Hash[8][8][8][8][8][8][8][8];
struct p
{
int x,y;
};
struct s
{
p node[4];
int step;
}st,ed,a,temp;
int cmp(p a,p b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
void biaoji(struct s a,int w)
{
Hash[a.node[0].x][a.node[0].y][a.node[1].x][a.node[1].y][a.node[2].x][a.node[2].y][a.node[3].x][a.node[3].y]=w;
}
char getnum(struct s a)
{
return Hash[a.node[0].x][a.node[0].y][a.node[1].x][a.node[1].y][a.node[2].x][a.node[2].y][a.node[3].x][a.node[3].y];
}
int dirx[4]={0,-1,0,1};
int diry[4]={1,0,-1,0};
int jud(s &a,int i,int j,int m)
{
if(m==1)
{
if(a.step>=4)
return 0;
a.step++;
}
a.node[i].x+=dirx[j];
a.node[i].y+=diry[j];
if(a.node[i].x<0||a.node[i].x>=8||a.node[i].y<0||a.node[i].y>=8)
return 0;
int k;
for(k=0;k<4;k++)
{
if(i!=k)
{
if(a.node[i].x==a.node[k].x&&a.node[i].y==a.node[k].y)
{
if(m==1)
{
return jud(a,i,j,2);
}
else
return 0;
}
}
}
if(k>=4)
{
sort(a.node,a.node+4,cmp);
return 1;
}
}
int bfs()
{
queue<struct s>q1;
queue<struct s>q2;
memset(Hash,0,sizeof(Hash));
sort(st.node,st.node+4,cmp);
sort(ed.node,ed.node+4,cmp);
st.step=0;
ed.step=0;
q1.push(st);
biaoji(st,1);
q2.push(ed);
biaoji(ed,2);
int tmp1,tmp2;
tmp1=tmp2=0;
while(!q1.empty()||!q2.empty())
{
if(!q1.empty())
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
a=q1.front();
if(jud(a,i,j,1))
{
char c=getnum(a);
if(c==2)
return 1;
if(c==0)
{
biaoji(a,1);
q1.push(a);
}
}
}
q1.pop();
}
//tmp1=q1.front().step;
if(!q2.empty())
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
a=q2.front();
if(jud(a,i,j,1))
{
char c=getnum(a);
if(c==1)
return 1;
if(c==0)
{
biaoji(a,2);
q2.push(a);
}
}
}
q2.pop();
}
}
return 0;
}
int main()
{
while(scanf("%d%d",&st.node[0].x,&st.node[0].y)!=EOF)
{
st.node[0].x--;
st.node[0].y--;
int i;
for(i=1;i<4;i++)
{
scanf("%d%d",&st.node[i].x,&st.node[i].y);
st.node[i].x--;
st.node[i].y--;
}
for(i=0;i<4;i++)
{
scanf("%d%d",&ed.node[i].x,&ed.node[i].y);
ed.node[i].x--;
ed.node[i].y--;
}
if(bfs())
printf("YES\n");
else
printf("NO\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: