| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 11271 | Accepted: 3469 |
Description
The game takes place on a rectangular board consisting of w * h squares. Each square might or might not contain a game piece, as shown in the picture.
One important aspect of the game is whether two game pieces can be connected by a path which satisfies the two following properties:
It consists of straight segments, each one being either horizontal or vertical.
It does not cross any other game pieces.
(It is allowed that the path leaves the board temporarily.)
Here is an example:
The game pieces at (1,3) and at (4, 4) can be connected. The game pieces at (2, 3) and (3, 4) cannot be connected; each path would cross at least one other game piece.
The part of the game you have to write now is the one testing whether two game pieces can be connected according to the rules above.
Input
Each description is followed by several lines containing four integers x1, y1, x2, y2 each satisfying 1 <= x1,x2 <= w, 1 <= y1,y2 <= h. These are the coordinates of two game pieces. (The upper left corner has the coordinates (1, 1).) These two game pieces will always be different. The list of pairs of game pieces for a board will be terminated by a line containing "0 0 0 0".
The entire input is terminated by a test case starting with w=h=0. This test case should not be procesed.
Output
Output a blank line after each board.
Sample Input
5 4 XXXXX X X XXX X XXX 2 3 5 3 1 3 4 4 2 3 3 4 0 0 0 0 0 0
Sample Output
Board #1: Pair 1: 4 segments. Pair 2: 3 segments. Pair 3: impossible.
Source
Mid-Central European Regional Contest 1999
博主代码:(提交的话会超时,找不到原因)
# include <stdio.h>
# include <string.h>
# define MAXN 75
# define INF 0xfffffff
char board[MAXN+2][MAXN+2];
bool mark[MAXN+2][MAXN+2];
int w, h, minstep;
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; //方向数组 上 左 下 右
void dfs(int x, int y, int end_x, int end_y, int step, int ori)
{
//如果当前步数大于最小的步数直接返回,进行一次优化
if (step > minstep)
{
return ;
}
// 当到达终点时
if (x == end_x && y == end_y)
{
if (step < minstep)
{
minstep = step;
}
return ;
}
for (int i = 0; i < 4; i++)
{
int next_x = x + dir[i][0];
int next_y = y + dir[i][1];
// 过滤掉超过板子宽度和高度的next_x和next_y
if (next_x < 0 || next_x > w + 2 || next_y < 0 || next_y > h + 2)
{
continue;
}
// 当路径是空格并且没有被标记过 或者当到达终点 时
if ((' ' == board[next_y][next_x] && !mark[next_y][next_x])
|| (next_x == end_x && next_y == end_y && 'X' == board[next_y][next_x]))
{
mark[next_y][next_x] = true;
if (ori == i)
{
dfs(next_x, next_y, end_x, end_y, step, i);
}
else
{
dfs(next_x, next_y, end_x, end_y, step+1, i);
}
mark[next_y][next_x] = false;
}
}
}
int main()
{
int board_count = 1;
while (scanf("%d %d", &w, &h), w && h)
{
memset(board, ' ', sizeof(board));
for (int i = 1; i <= h; i ++)
{
getchar();
for (int j = 1; j <= w; j++)
{
board[i][j] = getchar();
}
}
printf("Board #%d:\n", board_count);
int card_count = 1;
int start_x, start_y, end_x, end_y;
while (scanf("%d %d %d %d", &start_x, &start_y, &end_x, &end_y),
start_x && start_y && end_x && end_y)
{
minstep = INF;
memset(mark, false, sizeof(mark));
dfs(start_x, start_y, end_x, end_y, 0, -1);
if (INF == minstep)
{
printf("Pair %d: impossible.\n", card_count);
}
else
{
printf("Pair %d: %d segments.\n", card_count, minstep);
}
card_count ++;
}
board_count ++;
}
return 0;
}
其他博主的AC代码:https://blog.csdn.net/phoebe201421085/article/details/45117559(博客链接)
#include <iostream>
#include <stdio.h>
using namespace std;
int w,h,x1,x2,y1,y2,a[80][80],f[80][80],best,now,
dx[4]={0,0,-1,1},
dy[4]={-1,1,0,0};
void dfs(int xx,int yy,int dd)
{
int i,x,y;
if (now<best&&xx==x2&&yy==y2) { best=now;return;} /*到达终点,记录最小的*/
if (now>best) return;//大于最优跳出
for (i=0;i<=3;i++)
{
x=xx+dx[i]; //四个方向
y=yy+dy[i];
if ((f[x][y]==0&&x<=w+1&&y<=h+1&&x>=0&&y>=0)||(x==x2&&y==y2))//没有走过,没越界
{
f[x][y]=1; //标记走过
if (i!=dd) now++; //改变拐弯次数
dfs(x,y,i);
if (i!=dd) now--; //重置标记
f[x][y]=0;
//若方向不改变,继续走下去,不递归
}
}
}
int main()
{ int i,j,d,k,kk;
char c[80];
// freopen("in.txt","r",stdin);
cin>>h>>w;kk=0;
while (h>0&&w>>0)
{
kk++;
cout<<"Board #"<<kk<<":"<<endl;
k=0;
for (i=0;i<=w+1;i++)
for (j=0;j<=h+1;j++) a[i][j]=0;
getchar();
for (i=1;i<=w;i++)
{
gets(c);
for (j=1;j<=h;j++)
if (c[j-1]=='X') a[i][j]=1;
}
cin>>y1>>x1>>y2>>x2;
while (y1+x1+y2+x2>0)
{
k++;
best=9999999;
now=0;//当前拐弯次数
for (i=0;i<=w+1;i++)
for (j=0;j<=h+1;j++) f[i][j]=a[i][j];
// f[x2][y2]=0;
for (d=0;d<=3;d++) dfs(x1,y1,d);//d表示当前方向
cout<<"Pair "<<k<<": ";
if (best<9999999) cout<<best+1<<" segments."<<endl;else cout<<"impossible."<<endl;
//f[x2][y2]=1;
cin>>y1>>x1>>y2>>x2;
}
cout<<endl;
cin>>h>>w;
}
return 0;
}
509




被折叠的 条评论
为什么被折叠?



