The ChessTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 466 Accepted Submission (s): 130
Problem descriptionmr Li likes playing chess very much. One day he made a simplified version of the game. The chess board is relatively smaller. He'll first place is three kinds of chesses on his own behalf, namely the Chariot ("Ju"), the Horse ("Ma") and the Canno N ("Pao"). Then he placed some chesses on the opposite ' s behalf. There would is one and only one chess of the opposite marked as the general ("Shuai"). Mr Li wants to kill the general as soon as possible without killing other chesses.
All chesses would move in the same to as in real chess games (the Chinese chess).
To make the game easier, the opposite ' s chesses would stand still.
Inputfor each test case,the first line contains the integers n and m (2<=n,m<=10) indicating the size of the Borad. The next n lines, containing M characters each, describe the board. ' C ', ' M ' and ' P ' stand for the Chariot, the Horse and the Cannon on Mr Li's behalf. ' S ' stands for the general of the opposite while ' D ' stands for other chesses. Cells marked as '. ' Are empty.
Process to the end of file.
Outputfor each test case, first print a line saying "Scenario #k", where K was the number of the the test case. Then,if Mr Li can kill the general, print the least number of steps needed. Otherwise print the sentence "oh! That ' s impossible!. " Print a blank line after all test case, even after the last one.
Sample Input
5 5..DSD ... D.C .... P.d ..... M.7 7.DDSDD ... Ddd..... D........ P.. C........ M..........
Sample Output
Scenario #12Scenario #2OH! That ' s impossible!
Analysis: This is a very good search problem, I do not know why with CIN on WA, replaced by%s on AC, pit a day, pit Jiyan even Rice did not eat.
Wide search is to traverse the car, horse, cannon all the position state, open a 6-dimensional array marker 3 elements of the location, car, horse comparison good traverse, car 4 direction, horse 8 • Direction, cannon except can and car walk outside
Also can, also can "GE fight Cow". Note: The horse can be dialed horse leg, Cannon can't go to the handsome position.
code example:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace Std;
typedef struct
{
int xj,xm,xp;
int yj,ym,yp;
int step;
}node;
int map[11][11],dis[11][11][11][11][11][11];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int dit[8][2]={{2,1},{1,2},{-2,1},{1,-2},{2,-1},{-1,2},{-2,-1},{-1,-2}};
int dic[8][2]={{1,0},{0,1},{-1,0},{0,-1},{1,0},{0,1},{-1,0},{0,-1}};
int n,m,ax,ay,bx,by,cx,cy,x,y;
int min (int a,int b)
{
Return a<b?a:b;
}
int max (int a,int b)
{
Return a>b?a:b;
}
int BFS ()
{
int num,h,t;
Node Fir,nex;
FIR.XJ=AX,FIR.XM=BX,FIR.XP=CX;
Fir.yj=ay,fir.ym=by,fir.yp=cy;
Fir.step=0;
Dis[ax][ay][bx][by][cx][cy]=1;
queue<node>q;
Q.push (FIR);
while (! Q.empty ())
{
Fir=q.front ();
Q.pop ();
Map[fir.xj][fir.yj]=1;
Map[fir.xm][fir.ym]=1;
Map[fir.xp][fir.yp]=1;
for (int i=0;i<8;i++)
{
Nex=fir;
NEX.XM=FIR.XM+DIT[I][0];
NEX.YM=FIR.YM+DIT[I][1];
nex.step=fir.step+1;
if ((fir.xm+dic[i][0]==x) && (fir.ym+dic[i][1]==y)) continue;
if (Map[fir.xm+dic[i][0]][fir.ym+dic[i][1]]) continue;
if (Nex.xm>=1&&nex.xm<=n&&nex.ym>=1&&nex.ym<=m&&!map[nex.xm][nex.ym ]&&!DIS[NEX.XJ][NEX.YJ][NEX.XM][NEX.YM][NEX.XP][NEX.YP])
{
if (nex.xm==x&&nex.ym==y)
{
return nex.step;
}
Dis[nex.xj][nex.yj][nex.xm][nex.ym][nex.xp][nex.yp]=1;
Q.push (NEX);
}
}
for (int i=0;i<4;i++)
{
Nex=fir;
NEX.XJ=FIR.XJ+DIR[I][0];
NEX.YJ=FIR.YJ+DIR[I][1];
nex.step=fir.step+1;
while (1)
{
if (Nex.xj>=1&&nex.xj<=n&&nex.yj>=1&&nex.yj<=m&&!map[nex.xj][nex.yj ]&&!DIS[NEX.XJ][NEX.YJ][NEX.XM][NEX.YM][NEX.XP][NEX.YP])
{
if (nex.xj==x&&nex.yj==y)
{
return nex.step;
}
Dis[nex.xj][nex.yj][nex.xm][nex.ym][nex.xp][nex.yp]=1;
Q.push (NEX);
}
Else
{
Break
}
NEX.XJ+=DIR[I][0];
NEX.YJ+=DIR[I][1];
}
}
for (int i=0;i<4;i++)
{
Nex=fir;
NEX.XP=FIR.XP+DIR[I][0];
NEX.YP=FIR.YP+DIR[I][1];
nex.step=fir.step+1;
num=0;
while (1)
{
if (! ( NEX.XP>=1&&NEX.XP<=N&&NEX.YP>=1&&NEX.YP<=M)) break;
if (Map[nex.xp][nex.yp]) num++;
if (num>1) break;
if (nex.xp==x&&nex.yp==y&&num==0)
Break
if (nex.xp==x&&nex.yp==y&&num==1)
{
printf ("<%d,%d>\n", NEX.XP,NEX.YP);
return nex.step;
}
if (!map[nex.xp][nex.yp]&&!dis[nex.xj][nex.yj][nex.xm][nex.ym][nex.xp][nex.yp]&&num==0)
{
Dis[nex.xj][nex.yj][nex.xm][nex.ym][nex.xp][nex.yp]=1;
Q.push (NEX);
}
NEX.XP+=DIR[I][0];
NEX.YP+=DIR[I][1];
}
}
map[fir.xj][fir.yj]=0;
map[fir.xm][fir.ym]=0;
map[fir.xp][fir.yp]=0;
}
return-1;
}
int main ()
{
Char c[21][21];
int l=0;
while (~SCANF ("%d%d", &n,&m))
{
memset (dis,0,sizeof (dis));
for (int i=1;i<=n;i++)
scanf ("%s", c[i]+1);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
cin>>c;
if (c[i][j]== ' D ')
{
Map[i][j]=1;
}
Else
{
map[i][j]=0;
}
if (c[i][j]== ' S ')
{
X=i,y=j;
Continue
}
if (c[i][j]== ' C ')
{
Ax=i,ay=j;
Continue
}
if (c[i][j]== ' M ')
{
Bx=i,by=j;
Continue
}
if (c[i][j]== ' P ')
{
Cx=i,cy=j;
}
}
int Time=bfs ();
l++;
printf ("Scenario #%d\n", L);
if (time==-1)
{
printf ("oh! That ' s impossible!\n\n ");
}
Else
printf ("%d\n\n", time);
}
return 0;
}
HDU 3004 The Chess "wide-alone priority search"