UVA - 825 Walking on the Safe Side
来源:互联网 发布:手机淘宝删除差评步骤 编辑:程序博客网 时间:2024/05/16 10:10
额。别人都说这是简单dp,可我觉得这么做理论上是错的,只是这里的数据比较水而已。如果用f[i][j]=f[i-1][j]+f[i][j-1],否则f[i][j]=0。这种状态转移方程的话,只能满足题目图里面那种特殊情况,即,路径都只会向右和向下沿伸。如果是下面这种情况就不行了:
额,图片凑合着看吧,手残只会用画图的我也是挺拼的。。。
也就是说,出题者的意图就是考dp,可却没考虑严密。。。
不过开始时我也遇到不少坑。先看这题,感觉可以用dp,就用递归加记忆写了个,写完才发现思考的很不严密,关键是这样的题目没遇到过,只懂理论不会实践,怎么都调不对。。
只好换思路,才想到先用bfs计算距离,再用dfs计数正好可以,而且复杂度也在w*n,比dp也慢不了多少吧。。可我怎么交都tle...于是各种测试各种优化。。还是tle,几乎就在我百思不得其解为什么这么慢,要放弃准备再转到dp时,猛然想到可能输入有问题,用文件输入试了下果然如此。。。想来我以前是遇到过类似的输入的,为什么今天却不能这样处理了呢。。。于是我就ac了。。
#include<cstdio>#include<cstring>#include<algorithm>#define MAX 1200using namespace std;int w,n,dist[MAX][MAX],s[MAX*MAX][2],vector[4][2]={{-1,0},{0,-1},{0,1},{1,0},};char map[MAX][MAX],vis[MAX][MAX],in[2000];;int num(int i,int j){ int ans=0,help=1,t; for(t=i;t<=j;t++) { ans=(ans*help+in[t]-'0'); help*=10; } //printf("%d",ans); return ans;}void get(){ getchar(); int i,j,t,len; for(i=1;i<=w;i++) { gets(in); //puts(in); len=strlen(in); if(len>=3) { for(j=2,t=2;j<len;) { if((in[t]>='0'&&in[t]<='9')) { t++; } else{ map[i][num(j,t-1)]=1; //printf("%d %d",i,num(j,t-1)); j=t+1; t=j+1; } } } }}void bfs(){ int front,rear,i,j,xx,yy; front=0,rear=1; s[front][0]=w,s[front][1]=n,vis[w][n]=1; while(front<rear) { if(s[front][0]==1&&s[front][1]==1) break; for(i=0;i<4;i++) { xx=s[front][0]+vector[i][0],yy=s[front][1]+vector[i][1]; if(xx>=1&&xx<=w&&yy>=1&&yy<=n&&!vis[xx][yy]&&!map[xx][yy]) { vis[xx][yy]=1; s[rear][0]=xx,s[rear][1]=yy,dist[xx][yy]=dist[s[front][0]][s[front][1]]+1; rear++; } } front++; }}int dfs(int x,int y){ if(x==w&&y==n) return 1; int i,j,xx,yy,ans=0; for(i=0;i<4;i++) { xx=x+vector[i][0],yy=y+vector[i][1]; if(xx>=1&&xx<=w&&yy>=1&&yy<=n&&dist[xx][yy]+1==dist[x][y]&&!map[xx][yy]) ans+=dfs(xx,yy); } return ans;}int main(){ int t,i,j,num,flag; char ch; scanf("%d",&t); while(t--) { memset(map,0,MAX*MAX); memset(dist,0,MAX*MAX*4); memset(vis,0,MAX*MAX); scanf("%d %d",&w,&n); /*for(i=1;i<=w;i++) { flag=0; while(1) { scanf("%d",&num); ch=getchar(); if(flag!=0) map[i][num]=1; flag++; if(ch=='\n') break; } }*/ get(); bfs(); printf("%d\n",dfs(1,1)); if(t) printf("\n"); } return 0;}
0 0
- uva 825 Walking on the Safe Side
- UVA 825 Walking on the Safe Side
- UVa 825 - Walking on the Safe Side
- uva 825 - Walking on the Safe Side
- UVa 825 - Walking on the Safe Side
- UVA-825-Walking on the Safe Side
- UVa:825 Walking on the Safe Side
- UVA - 825 Walking on the Safe Side
- uva 825 Walking on the Safe Side
- UVa 825 - Walking on the Safe Side
- UVA - 825 Walking on the Safe Side
- UVA - 825 Walking on the Safe Side
- uva 825 Walking on the Safe Side
- UVA 825 Walking on the Safe Side
- UVA - 825 Walking on the Safe Side
- dp uva-825-Walking on the Safe Side
- UVa 825 Walking on the Safe Side (DP)
- UVA 825 Walking on the Safe Side(记忆化搜索)
- [POJ 2431]Expedition
- 万物归于静
- struts2中实现文件上传功能
- 计算24点的简单程序
- 单调队列典型例题——广告印刷
- UVA - 825 Walking on the Safe Side
- XML序列化
- 针对SVG的性能优化
- 逻辑回归不错的文章
- AT&T/UNIX 与Intel 汇编语法区别
- jsp中session的判断用户是否登陆问题?
- hdu1032 3n+1 Problem
- HDU 5074 Hatsune Miku
- SIM900A基站定位调试笔记