搜索
见破题:
思路十分简单,至于代码嘛...反正我是改了一上午...
这里就想说一个搜索的通用方法,可以用f[i][j]表示某个状态,然后如果搜索到相同的坐标,但价值没有之前存的好,就直接退出...
至于此题就是(i,j)点的最大价值...
#include<bits/stdc++.h> #define max(a,b) (((a)>(b)?(a):(b))) using namespace std; int h,v,w,qx,qy,zx,zy,dx[5]={1,2,3,4},d,f[50][50][5],ans;//f[i][j]表示到(i,j)点的最大速度. char ch[50][50];//1-左,2-右 inline int read() { int x=0,ff=1; char ch=getchar(); while(!isdigit(ch)){if(ch=='-') ff=-1;ch=getchar();} while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} return x*ff; } inline void dfs(int x,int y,int d,int v,int m) { //printf("x=%d y=%d d=%d v=%d\n",x,y,d,v); if(v<=f[x][y][d]) return;f[x][y][d]=v; if(ch[x][y]=='F') return; if(d==1) { int pd=0; if(x-1>=1&&ch[x-1][y]!='.') pd=1,dfs(x-1,y,1,v+1,0); if(m!=1&&y-1>=1&&ch[x][y-1]!='.') pd=1,dfs(x,y,4,max(0,v-35),1); if(m!=2&&y+1<=w&&ch[x][y+1]!='.') pd=1,dfs(x,y,2,max(0,v-40),2); if(!pd&&x+1<=h&&ch[x+1][y]!='.') dfs(x,y,3,0,0); } else if(d==2) { int pd=0; if(y+1<=w&&ch[x][y+1]!='.') pd=1,dfs(x,y+1,2,v+1,0); if(m!=1&&x-1>=1&&ch[x-1][y]!='.') pd=1,dfs(x,y,1,max(0,v-35),1); if(m!=2&&x+1<=h&&ch[x+1][y]!='.') pd=1,dfs(x,y,3,max(0,v-40),2); if(!pd&&y-1>=1&&ch[x][y-1]!='.') dfs(x,y,4,0,0); } else if(d==3) { int pd=0; if(x+1<=h&&ch[x+1][y]!='.')pd=1,dfs(x+1,y,3,v+1,0); if(m!=1&&y+1<=w&&ch[x][y+1]!='.')pd=1,dfs(x,y,2,max(0,v-35),1); if(m!=2&&y-1>=1&&ch[x][y-1]!='.')pd=1,dfs(x,y,4,max(0,v-40),2); if(!pd&&x-1>=1&&ch[x-1][y]!='.') dfs(x,y,1,0,0); } else if(d==4) { int pd=0; if(y-1>=1&&ch[x][y-1]!='.')pd=1,dfs(x,y-1,4,v+1,0); if(m!=1&&x+1<=h&&ch[x+1][y]!='.')pd=1,dfs(x,y,3,max(0,v-35),1); if(m!=2&&x-1>=1&&ch[x-1][y]!='.')pd=1,dfs(x,y,1,max(0,v-40),2); if(!pd&&y+1<=w&&ch[x][y+1]!='.') dfs(x,y,2,0,0); } } int main() { freopen("1.in","r",stdin); h=read();w=read();v=read(); for(register int i=1;i<=h;++i) for(register int j=1;j<=w;++j) { cin>>ch[i][j]; if(ch[i][j]=='F') zx=i,zy=j; else if(ch[i][j]!='.'&&ch[i][j]!='#') { qx=i;qy=j; if(ch[i][j]=='N') d=1; else if(ch[i][j]=='E') d=2; else if(ch[i][j]=='S') d=3; else if(ch[i][j]=='W') d=4; } } for(register int i=1;i<=h;++i) for(register int j=1;j<=w;++j) for(register int k=1;k<=4;k++) f[i][j][k]=-1e9; dfs(qx,qy,d,v,0); for(register int i=1;i<=4;++i) ans=max(ans,f[zx][zy][i]); printf("%d\n",ans); return 0; }