U-Turn (深搜)
来源:互联网 发布:什么时候开放网络购彩 编辑:程序博客网 时间:2024/06/04 18:25
Given the map of a town with R x C cells, and each cell is:
X: building segment
.: a road surface
Each turn, you can move (up, down, left, or right) from a road surface to the neighboring road surface.
Now, you start from any possible road surface cells as the starting cell, and move to any possible directions, can you return to the starting cell without making a 180 degrees turn (U-turn)?
input
The first line contains two positive integers R and C (3 ≤ R, C ≤ 10).
Then follows R lines, each line has C characters, each is either “X” or “.”, representing each cell of the map.
There are at least two "." and all "." characters are connected into some roads.
Output 0 if you can return to the starting cell for any moves without making a 180 degrees turn. Otherwise output 1.
sample input
4 3
XXX
X.X
X.X
XXX
sample output
1
attention
Another case:
4 4
XXXX
X..X
X..X
XX.XYou should output 1 because if you start from any of the road surface cell and move to the 4th row and the 3rd column cell, then you can not return back.
题意:在一个所给的地图中,一辆不会拐弯的车能不能行驶回到出发点。
分析:一看到这样的题目,基本上就是深搜的做法,这道题目用深搜可以解决,还有一个比较特殊的做法也可以ac.
普通的深度搜索
#include<stdio.h>char str[15][15];char mod[15][15];int n,m,flag=1;int e=0;void modle(){for(int i=0;i<n;i++){for(int j=0;j<m;j++){mod[i][j]=str[i][j];}}}int dfs(int i,int j){e++; if(e>2&&(mod[i+1][j]=='?'||mod[i-1][j]=='?'||mod[i][j+1]=='?'||mod[i][j-1]=='?')) flag=0; if(mod[i][j]!='?') mod[i][j]='X'; if(mod[i+1][j]=='.') { dfs(i+1,j); } if(mod[i-1][j]=='.') { dfs(i-1,j); } if(mod[i][j-1]=='.') { dfs(i,j-1); } if(mod[i][j+1]=='.') { dfs(i,j+1); }}int main(){scanf("%d %d",&n,&m);for(int i=0;i<n;i++){scanf("%s",str[i]);}for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(str[i][j]=='.'){modle();mod[i][j]='?';dfs(i,j); if(flag==1) { printf("1\n"); return 0; }flag=1;e=0;}}}printf("0\n");return 0;}
特殊的解决办法
#include<cstdio>#include<string>#include<iostream>#include<algorithm>using namespace std;char map[12][12];int dfs(int r,int c){int flag=0;for(int i=1;i<=r;i++){for(int j=1;j<=c;j++){int sum=0;if(map[i][j]=='.'){if(map[i-1][j]=='.')sum++;if(map[i+1][j]=='.')sum++;if(map[i][j-1]=='.')sum++;if(map[i][j+1]=='.')sum++;if(sum==1)flag=1;}}}return flag;}int main(){int i,j,r,c;scanf("%d %d",&r,&c);for(i=1;i<=r;i++){getchar();for(j=1;j<=c;j++)scanf("%c",&map[i][j]);}printf("%d\n",dfs(r,c));}
- U-Turn (深搜)
- 解决U-turn问题的Dijkstra算法(基于实际道路交通网络)
- Turn the corner(三分)
- Turn the corner(三分)
- The Byrds - Turn! Turn! Turn!
- heru 5081 Turn the corner(三分)
- 网络分析之转弯要素(Turn Features)
- 13.04.07 Turn the corner (三分)
- 网络分析之转弯要素(Turn Features)
- POJ 3106Flip and Turn(模拟)
- hdu 2438Turn the corner(三分)
- hdu 4869 Turn the pokers (思维)
- HDU 2438 Turn the corner(三分)
- acdream 1043 Always Turn Left(模拟)
- hdu 2438 Turn the corner(三分)
- 2438 Turn the corner(数学题,三分法)
- CodeForces 630 M. Turn(水~)
- HDU 2438 Turn the corner(三分)
- 解决Can’t locate ExtUtils/MakeMaker.pm in @INC
- qmake: could not exec ‘/usr/lib/x86_64-linux-gnu/qt4/bin/qmake’: No such file or directory 解决方案
- 跳跃游戏
- 学习开发单片机的8个重点
- jquery-editable-select设置显示默认选项
- U-Turn (深搜)
- Observed package id 'build-tools;17.0.0' in inconsistent
- 调试之道
- 使用jenkins实现持续集成
- 【整理】【Learn Git Branching】02 高级篇
- java基础正则、XML
- 8.12 俄罗斯方块 1863
- XZ_iOS之异步裁切绘制圆角图形
- 网页以FTP方式发布到Server的粗略过程