ZOJ 1148

这道题目要求最少多少线段,广搜的过程中我们将处于同一直线上的步数设为同样的值(因为他们可以连成一条线,也就是只有一条线段),我们搜索时候,在同一个方向上一直往前走
#include<iostream>
#include<queue>
#include<memory.h>
#include<stdio.h>
using namespace std;

char ch;
int n,m,sx,sy,dx,dy;
int mat[80][80];
int d[80][80];
int dir[4][2]={{0,1},{-1,0},{0,-1},{1,0}};

struct Node{
	int x,y;
};
queue<Node> q;
Node nd[6400];

int main(){
	int board=1;
	while(cin>>n>>m){
		if(m==0&&n==0)
			break;
		cout<<"Board #"<<board++<<":"<<endl;
		getchar();
		//保证了拓宽的边界为0
		memset(mat,0,sizeof(mat));
		for(int i=1;i<=m;i++){
			for(int j=1;j<=n;j++){
				scanf("%c",&ch);
				if(ch=='X')
					mat[i][j]=1;
			}
			getchar();
		}

		int pair=1;
		while(cin>>sy>>sx>>dy>>dx){
			if(sx==0&&sy==0&&dx==0&&dy==0)
				break;
			while(!q.empty())
			    q.pop();
			memset(d,-1,sizeof(d));
			mat[sx][sy]=mat[dx][dy]=0;
			d[sx][sy]=0;
			Node st;
			st.x=sx,st.y=sy;
			q.push(st);

			while(!q.empty()){
				Node tmp=q.front();
				q.pop();
				for(int i=0;i<4;i++){
					 int a=tmp.x+dir[i][0];
					 int b=tmp.y+dir[i][1];
					 while(a>=0&&a<=m+1&&b>=0&&b<=n+1&&mat[a][b]==0&&d[a][b]==-1){
							st.x=a;
							st.y=b;
							q.push(st);
							d[a][b]=d[tmp.x][tmp.y]+1;
							a+=dir[i][0];
							b+=dir[i][1];
					 }
				}
			}//end while
			mat[sx][sy]=mat[dx][dy]=1;

			if(d[dx][dy]==-1)
				cout<<"Pair "<<pair++<<": "<<"impossible."<<endl;
			else
				cout<<"Pair "<<pair++<<": "<<d[dx][dy]<<" segments."<<endl;
		}
		cout<<endl;//忘记了
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值