题目链接:https://ac.nowcoder.com/acm/contest/331/I
按找题意模拟就好了...代码略丑,不想改了...
AC代码:
#include <bits/stdc++.h>
using namespace std;
string str[505];
int n,m,pos;
bool flag;
void dfs(int x,int y,int h){
while(1){
if(x == -1 || y == -1 || x == n || y == m){
if(x == n) flag = true, pos = y + 1;
break;
}
// cout<<x<<" "<<y<<endl;
while(x >= 0 && y >= 0 && x < n && y < m && str[x][y] != '.'){
// cout<<x<<" "<<y<<endl;
if(str[x][y] == '/'){
if(h == 0){ y = y - 1, h = 2; continue;}
if(h == 1){ y = y + 1, h = 3; continue;}
if(h == 2){ x = x + 1, h = 0; continue;}
if(h == 3){ x = x - 1, h = 1; continue;}
// continue;
}
if(str[x][y] == '\\'){
if(h == 0){ y = y + 1; h = 3; continue;}
if(h == 1){ y = y - 1; h = 2; continue;}
if(h == 2){ x = x - 1; h = 1; continue;}
if(h == 3){ x = x + 1; h = 0; continue;}
}
// cout<<x<<" "<<y<<endl;
}
// cout<<x<<" "<<y<<endl;
if(x >= 0 && y >= 0 && x < n && y < m && str[x][y] == '.'){
if(h == 0)x = x + 1;
if(h == 1)x = x - 1;
if(h == 2)y = y - 1;
if(h == 3)y = y + 1;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
cin>>str[i];
}
for(int i=0;i<m;i++){
flag = false;
dfs(0,i,0);
if(flag == true)cout<<pos<<endl;
else puts("-1");
}
return 0;
}