【题目大意】题目描述的就是地铁跑酷的场景,主角向右走,或者向左走。每次移动,都是主角先向右移动一格,再选择向上或向下或直行(注意这个先后关系),然后火车再向左移动两格。我们可以从相对移动的角度去看,火车向左移动就相当于主角向右移动两格。这样就不用管火车了,只考虑主角移动就行了。 【程序】
#include<iostream>#include<cstdio>#include<string>#include<cstring>using namespace std;char s[10][1000];int g[10][1000];int x, y;int n, m, T;int dfs(int x, int y){ bool f = 0; // cout << x << " " << y << endl; g[x][y] = 1; if(x > 3 || x < 1) return 0; if(y >= n) return 1; if(s[x][y + 1] == '.' && s[x][y + 2] == '.' && s[x][y + 3] == '.' && g[x][y + 3] == 0) f = f || dfs(x, y + 3); if(f) return f; if(s[x][y + 1] == '.' && s[x - 1][y + 1] == '.' && s[x - 1][y + 2] == '.' && s[x - 1][y + 3] == '.' && g[x - 1][y + 3] == 0) f = f || dfs(x - 1, y + 3); if(f) return f; if(s[x][y + 1] == '.' && s[x + 1][y + 1] == '.' && s[x + 1][y + 2] == '.' && s[x + 1][y + 3] == '.' && g[x + 1][y + 3] == 0) f = f || dfs(x + 1, y + 3); return f;}int main(){ scanf("%d", &T); while(T--){ for(int i = 0; i <= 4; i++){ for(int j = 0; j <= 200; j++) s[i][j] = '.'; } memset(g, 0, sizeof(g)); scanf("%d%d", &n, &m); // cout << n << endl; for(int i = 1; i <= 3; i++){ scanf("%s", s[i]); if(s[i][0] == 's'){ x = i; } s[i][n] = '.'; } if(dfs(x, 0)) puts("YES"); else puts("NO"); }}新闻热点
疑难解答