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;
}