您的位置:首页 > 其它

poj 3009 -- Curling 2.0

2014-08-03 09:57 337 查看
Curling 2.0

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 11389Accepted: 4818
Description

On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.

Fig. 1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.

/*======================================================================
*           Author :   kevin
*         Filename :   Curling2.cpp
*       Creat time :   2014-08-02 11:17
*      Description :
========================================================================*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#define M 25
#define INF 0x7f7f7f7f
using namespace std;
int grap[M][M];
int w,h;
int sx,sy,ex,ey,ans;
int d[4] = {0,1,2,3};
void DFS(int x,int y,int cnt,int d)
{
int i,j;
if(cnt > 10) return ;
if(x == ex && y == ey){
if(ans > cnt){
ans = cnt;
}
return ;
}
if(d == 0){
if(grap[x-1][y] == 1) return;
for(i = x; i >= 1; i--){
if(grap[i][y] == 1){
grap[i][y] = 0;
for(j = 0; j < 4; j++){
//if(d != j)
DFS(i+1,y,cnt+1,j);
}
grap[i][y] = 1; break;
}
if(grap[i][y] == 3) DFS(i,y,cnt,d);
}
}
if(d == 2){
if(grap[x+1][y] == 1) return;
for(i = x; i <= h; i++){
if(grap[i][y] == 1){
grap[i][y] = 0;
for(j = 0; j < 4; j++){
//if(d != j)
DFS(i-1,y,cnt+1,j);
}
grap[i][y] = 1; break;
}
if(grap[i][y] == 3) DFS(i,y,cnt,d);
}
}
if(d == 1){
if(grap[x][y+1] == 1) return;
for(i = y; i <= w; i++){
if(grap[x][i] == 1){
grap[x][i] = 0;
for(j = 0; j < 4; j++){
//if(d != j)
DFS(x,i-1,cnt+1,j);
}
grap[x][i] = 1; break;
}
if(grap[x][i] == 3) DFS(x,i,cnt,d);
}
}
if(d == 3){
if(grap[x][y-1] == 1) return;
for(i = y; i >= 1; i--){
if(grap[x][i] == 1){
grap[x][i] = 0;
for(j = 0; j < 4; j++){
//if(d != j)
DFS(x,i+1,cnt+1,j);
}
grap[x][i] = 1; break;
}
if(grap[x][i] == 3) DFS(x,i,cnt,d);
}
}
}
int main(int argc,char *argv[])
{
while(scanf("%d%d",&w,&h)!=EOF && w+h){
memset(grap,-1,sizeof(grap));
for(int i = 1; i <= h; i++){
for(int j = 1; j <= w; j++){
scanf("%d",&grap[i][j]);
if(grap[i][j] == 2){
sx = i; sy = j;
}
if(grap[i][j] == 3){
ex = i; ey = j;
}
}
}
ans = INF;
for(int i = 0; i < 4; i++){
DFS(sx,sy,1,d[i]);
}
if(ans == INF){
printf("-1\n");
}
else{
printf("%d\n",ans);
}
}
return 0;
}


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