题目描述
给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过。给定起点坐标和终点坐标,问每个方格最多经过1次,在迷宫中移动有上下左右四种方式。保证起点上没有障碍。问:有多少种从起点坐标到终点坐标的方案?
输入
第一行N、M和T,N为行,M为列,T为障碍总数。
第二行起点坐标SX,SY,终点坐标FX,FY。
接下来T行,每行为障碍的坐标。
输出
给定起点坐标和终点坐标,问每个方格最多经过1次,从起点坐标到终点坐标的方案总数。
样例输入
2 2 1
1 1 2 2
1 2
样例输出
1
数据范围限制
对于 50% 的数据,1<=N,M<=3;
对于 100% 的数据,1<=N,M<=5。
代码:
V1.0的代码(70分):
#include<bits/stdc++.h>
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int c_map[55][55],n,m,tot;
int Map[55][55];
int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};
void dfs(int x,int y,int lx,int ly)
{
if(x==lx&&y==ly)
{
tot++;
return ;
}
for(int i=0;i<4;i++)
{
int tx=dx[i]+x;
int ty=dy[i]+y;
if(tx>=0&&ty>=0&&tx<n&&ty<m&&c_map[tx][ty]==0&&Map[tx][ty]==0)
{
c_map[tx][ty] = 1;
dfs(tx,ty,lx,ly);
c_map[tx][ty] = 0;
}
}
}
int main()
{
int sx,sy,lx,ly,t;
cin >> n >> m >> t;
cin >> sx >> sy >> lx >> ly;
_for(i,0,t)
{
int x,y;
cin >> x >> y;
Map[x-1][y-1] = 1;
}
dfs(sx-1,sy-1,lx-1,ly-1);
cout<<tot;
return 0;
}
代码错误的测试点:
Input:
4 4 0
3 3 3 4
Output:
86
但是我输出的是305
V2.0的代码:
#include<bits/stdc++.h>
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int c_map[55][55],n,m,tot;
int Map[55][55];
int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};
void dfs(int x,int y,int lx,int ly)
{
if(x==lx&&y==ly)
{
tot++;
return ;
}
for(int i=0;i<4;i++)
{
int tx=dx[i]+x;
int ty=dy[i]+y;
if(tx>=0&&ty>=0&&tx<n&&ty<m&&c_map[tx][ty]==0&&Map[tx][ty]==0)
{
c_map[tx][ty] = 1;
dfs(tx,ty,lx,ly);
c_map[tx][ty] = 0;
}
}
}
int main()
{
int sx,sy,lx,ly,t;
cin >> n >> m >> t;
cin >> sx >> sy >> lx >> ly;
_for(i,0,t)
{
int x,y;
cin >> x >> y;
Map[x-1][y-1] = 1;
}
c_map[sx-1][sy-1] = 1;
dfs(sx-1,sy-1,lx-1,ly-1);
cout<<tot;
return 0;
}
PS:
所以大家一定要注意!dfs走迷宫的时候一定要标记开始位置,不然错在哪里都不知道!