hdu 1401

时间:2023-03-09 18:46:59
hdu 1401

Solitaire

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3077    Accepted Submission(s): 954

Problem Description
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).

hdu 1401

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.

Input
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.
Output
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.
Sample Input
4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6
Sample Output
YES
Source
Recommend
四个点的哈希,由于这四个点没有什么区分,哈希的时候注意一下。
由于的用set哈希,用*10的方法。所以要先排序。很容易理解的。
bfs的代码,有点意思。
 /**
4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6 **/ #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<set>
using namespace std; struct node
{
int x[];
int y[];
};
struct node start,end1; queue<node>Q[];
set<int>hxl[];
bool flag;
int map1[][]={{,},{,},{-,},{,-}}; bool fun(node &t,int i)
{
int j;
if(t.x[i]>=&&t.x[i]<= && t.y[i]>=&&t.y[i]<=)
{
for(j=;j<;j++)
{
if(j==i)continue;
if(t.x[i]==t.x[j] && t.y[i]==t.y[j]) return true;
}
return false;
}
return true;
}
void pai(node &t)
{
int i,j,x;
for(i=;i<; i++)
{
x=i;
for(j=i+;j<; j++)
if(t.x[x]>t.x[j])
x=j;
else if(t.x[x]==t.x[j] && t.y[x]>t.y[j])
x=j;
swap(t.x[i],t.x[x]);
swap(t.y[i],t.y[x]);
} }
int serch(node &t)
{
int i,sum=;
for(i=;i<;i++)
sum=sum*+t.x[i]*+t.y[i];
return sum;
}
void bfs(int x)
{
int i,j,size1,k;
node cur,t;
size1=Q[x].size();
while(size1--)
{
cur=Q[x].front();
Q[x].pop();
for(i=;i<;i++)/** every four point **/
{
for(j=;j<;j++) /** n s w e**/
{
t=cur;
t.x[i]=t.x[i]+map1[j][];
t.y[i]=t.y[i]+map1[j][];
if(fun(t,i)==true)
{
t.x[i]=t.x[i]+map1[j][];
t.y[i]=t.y[i]+map1[j][];
if(fun(t,i)==true) continue;
}
pai(t);
k=serch(t);
if(hxl[x].count(k)>)continue;
if(hxl[x^].count(k)>)
{
flag=true;
return;
}
hxl[x].insert(k);
Q[x].push(t);
}
}
}
}
void dbfs()
{
int ans=,k;
pai(start);
k=serch(start);
hxl[].insert(k);
Q[].push(start); pai(end1);
Q[].push(end1);
k=serch(end1);
hxl[].insert(k);
while(true)
{
if(Q[].size()<Q[].size())
bfs();
else bfs();
ans++;
if(ans==)break;
if(flag==true) return;
}
}
int main()
{
int i;
while(scanf("%d%d",&start.x[],&start.y[])>)
{
for(i=;i<;i++)
scanf("%d%d",&start.x[i],&start.y[i]);
for(i=;i<;i++)
scanf("%d%d",&end1.x[i],&end1.y[i]);
while(!Q[].empty()){
Q[].pop();
}
while(!Q[].empty()){
Q[].pop();
}
hxl[].clear();
hxl[].clear();
flag=false;
dbfs();
if(flag==true) printf("YES\n");
else printf("NO\n");
}
return ;
}