终于A了 P06614迷宫游戏5,原来是动态规划......
点击查看代码
#include<bits/stdc++.h>
#define debug(a); cout<<#a<<"="<<a<<endl;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
int n;
int m;
int t;
int r1,c1,r2,c2;
char mapp[1010][1010];
int dtgh[110][110][110];
int xx[5]={0,1,0,-1,0};
int yy[5]={0,0,1,0,-1};
struct node
{
int x;
int y;
int ans;
node(int x1,int y1,int ans1)
{
x=x1;
y=y1;
ans=ans1;
}
};
int mgbfs()
{
queue<node> q;
q.push(node(r1,c1,0));
while(!q.empty())
{
node now=q.front();
q.pop();
if(now.ans>t) continue;
rep(i,1,4)
{
int r=now.x+xx[i];
int c=now.y+yy[i];
if(r>=1&&r<=n&&c>=1&&c<=m&&mapp[r][c]!='*')
{
dtgh[r][c][now.ans+1]+=dtgh[now.x][now.y][now.ans];
if(dtgh[r][c][now.ans+1]-dtgh[now.x][now.y][now.ans]>0)
{
continue;
}
q.push(node(r,c,now.ans+1));
}
}
}
return dtgh[r2][c2][t];
}
int main()
{
cin>>n>>m>>t;
rep(i,1,n)
{
rep(j,1,m)
{
cin>>mapp[i][j];
}
}
cin>>r1>>c1>>r2>>c2;
dtgh[r1][c1][0]=1;
cout<<mgbfs();
return 0;
}