SGU 484. Kola DFS
484. Kola
Memory limit: 65536 kilobytes
output: standard
One day Vasya decided to buy a bottle of his favourite Kola in the old vending machine. But he forgot that there was a Halloween that day, so evil forces had made an inner structure of the vending machine quite complicated. Inside the machine is represented by a table of size n × m. Some of the cells are empty, and some contains obstacles of two types: '/' and '\'. One of the cell initially contains a bottle of Kola. After the purchasing it starts to fall vertically down by the following rules:
- If a cell immediately below the bottle is empty, the bottle falls down.
- If the bottle falls down from the lowest row, it falls to the tray and Vasya can take it.
- Reaching and obstacle '/' ('\') the bottle moves left (right) without changing it's row and tries to continue to fall down if it is possible.
- The bottle stops to move when there is a wall in the current moving direction.
- The bottle stops to move when it moves from the cell with an obstacle of one type to the cell with an obstacle of another type.
- But if the bottle moves to the cell with the same type of obstacle as in the current cell, it continues moving down.
Help Vasya to find out whether the bottle will reach the tray. In case of a positive answer, determine the number of column where it will happen.
The first line of the input contains two integer numbers n and m (1 ≤ n, m ≤ 100). Then the description of the vending machine follows. It consists of n lines of m characters each: '.' means empty cell, 'P' means initial position of the bottle, '/' and '\' — mean obstacles of the corresponding type. It is guaranteed that the 'P' character appears exactly once.
Print to the output -1 if the bottle doesn't reach the tray. Otherwise print the number of the column where the bottle will leave the vending machine. Columns are numbered starting from 1 from the leftmost one.
sample input |
sample output |
2 3 ./P ../ |
2 |
sample input |
sample output |
2 2 .P \/ |
-1 |
sample input |
sample output |
5 4 .P.. .\.. .//. ./.. /... |
-1 |
/* * Author: * Created Time: 2013/9/18 12:56:55 * File Name: A.cpp * solve: A.cpp */ #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<string> #include<map> #include<stack> #include<set> #include<iostream> #include<vector> #include<queue> //ios_base::sync_with_stdio(false); //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define sz(v) ((int)(v).size()) #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define repf(i, a, b) for (int i = (a); i <= (b); ++i) #define repd(i, a, b) for (int i = (a); i >= (b); --i) #define clr(x) memset(x,0,sizeof(x)) #define clrs( x , y ) memset(x,y,sizeof(x)) #define out(x) printf(#x" %d\n", x) #define sqr(x) ((x) * (x)) typedef long long LL; const int INF = 1000000000; const double eps = 1e-8; const int maxn = 200; int sgn(const double &x) { return (x > eps) - (x < -eps); } char maze[maxn][maxn]; int n,m; int flag; int ans; void dfs(int x,int y) { if(x == n) { flag = 1; ans = y; return ; } if(flag) return ; if(y < 0 || y >= m) return ; if(maze[x][y] == 'P' || maze[x][y] == '.') { dfs(x + 1,y); } else if(maze[x][y] == '/') { if(y>=1 && maze[x][y-1] == '.') dfs(x,y-1); else if(y>=1 && maze[x][y-1] == '/') dfs(x+1,y-1); else return ; }else if(maze[x][y] == '\\') { if(y+1 < m && maze[x][y+1] == '.') dfs(x,y+1); else if(y+1 < m && maze[x][y+1] == '\\') dfs(x+1,y+1); else return ; } } int main() { // freopen("in.txt","r",stdin); while(scanf("%d%d",&n,&m)==2) { rep(i,0,n) { scanf("%s",maze[i]); } int a,b; rep(i,0,n) rep(j,0,m) { if(maze[i][j] == 'P') { a = i; b = j; break; } } flag = 0; dfs(a,b); if(flag == 0) cout<<-1<<endl; else cout<<ans+1<<endl; } return 0; }
posted on 2013-10-14 22:38 keep trying 阅读(199) 评论(0) 收藏 举报