luogu P1605 迷宫

https://www.luogu.org/problem/P1605

题目背景

给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过。给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案。在迷宫中移动有上下左右四种方式,每次只能移动一个方格。数据保证起点上没有障碍。

题目描述

输入格式

第一行N、M和T,N为行,M为列,T为障碍总数。第二行起点坐标SX,SY,终点坐标FX,FY。接下来T行,每行为障碍点的坐标。

输出格式

给定起点坐标和终点坐标,问每个方格最多经过1次,从起点坐标到终点坐标的方案总数。

输入输出样例

输入 #1

2 2 1
1 1 2 2
1 2

输出 #1

1

说明/提示

【数据规模】

1≤N,M≤5

代码

#include<iostream>
#include<queue>
using namespace std;

const int N=5;
const int M=5;
int a[N+1][M+1];
bool visited[N+1][M+1];
int sx,sy,ex,ey;
int n,m,t;
int cnt=0;

struct Point{
	int x,y;
	Point(int xx,int yy):x(xx),y(yy){
	}
};

queue <Point> q;

void bfs()
{
	int tx,ty;
	tx=sx;
	ty=sy;
	
	q.push(Point(tx,ty));
	while(!q.empty())
	{
		Point t=q.front();q.pop();
		if(t.x==ex && t.y==ey)
		{
			cnt++;
			continue;
		}
		
		visited[t.x][t.y]=true;
		
		tx=t.x;
		ty=t.y+1;
		if(ty<=m && !visited[tx][ty] && a[tx][ty]!=1)
		{
			q.push(Point(tx,ty));
		}
		
		tx=t.x+1;
		ty=t.y;
		if(tx<=n && !visited[tx][ty] && a[tx][ty]!=1)
		{
			q.push(Point(tx,ty));
		}
		
		tx=t.x;
		ty=t.y-1;
		if(ty>=1 && !visited[tx][ty] && a[tx][ty]!=1)
		{
			q.push(Point(tx,ty));
		}
		
		tx=t.x-1;
		ty=t.y;
		if(tx>=1 && !visited[tx][ty] && a[tx][ty]!=1)
		{
			q.push(Point(tx,ty));
		}
	}
}

int main()
{
	cin>>n>>m>>t;
	
	cin>>sx>>sy>>ex>>ey;
	int x,y;
	for(int i=0; i<t; i++)
	{
		cin>>x>>y;
		a[x][y]=1;
	}
	
	bfs();
	
	cout<<cnt;
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值