标签:space name cin ems bit 说明 bsp freopen hdu
要注意题目给的输入顺序,
dfs里面第三条如果不共线并且相应步数已经到了的话
#include <bits/stdc++.h> using namespace std; int t,m,n,k,x1,yy1,x2,y2; char mp[110][110]; int turn[110][110]; int flag; int dx[4] = {0,0,-1,1}; int dy[4] = {-1,1,0,0}; void dfs(int x,int y,int dir){ if(x == x2 && y == y2 && turn[x][y] <= k){ flag = 1; return; } if(turn[x][y] > k) return; if(x != x2 && y != y2 && turn[x][y] == k) return; for(int i = 0; i < 4; i++){ int xx = x + dx[i]; int yy = y + dy[i]; if(xx <= 0 ||xx > m || yy <= 0 || yy > n || mp[xx][yy] == ‘*‘) continue; //xx yy 已经走过了,并且上次走的要比这次小 if(turn[xx][yy] < turn[x][y]) continue; //需要转弯,并且转弯之后还小,说明之前走过 if(dir != -1 && i != dir && turn[xx][yy] < turn[x][y] + 1) continue; if(dir != -1 && i != dir) turn[xx][yy] = turn[x][y] + 1; else turn[xx][yy] = turn[x][y]; mp[xx][yy] = ‘*‘; dfs(xx,yy,i); mp[xx][yy] = ‘.‘; if(flag) return; } } int main(){ //freopen("in","r",stdin); ios::sync_with_stdio(0); cin >> t; while(t--){ cin >> m >> n; for(int i = 1; i <= m; i++){ for(int j = 1; j <= n; j++) cin >> mp[i][j]; } cin >> k >> yy1 >> x1 >> y2 >> x2; memset(turn,0x3f, sizeof(turn)); flag = 0; turn[x1][yy1] = 0; dfs(x1,yy1,-1); if(flag) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
标签:space name cin ems bit 说明 bsp freopen hdu
原文地址:https://www.cnblogs.com/xcfxcf/p/12680848.html