fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define y1 aaaa
  4. int n, m, y1, x1, y2, x2, visited[301][301], y, x, cnt = 1;
  5. char a[301][301];
  6. bool isEnd;
  7. vector<pair<int, int>> v;
  8. queue<pair<int,int>> q;
  9. int dy[] = {-1, 0, 1, 0};
  10. int dx[] = {0, 1, 0, -1};
  11.  
  12. void dfs(int y, int x){
  13. if(y == y2 - 1 && x == x2 - 1){
  14. isEnd = true;
  15. return;
  16. }
  17.  
  18. for(int i = 0; i < 4; i++){
  19. int ny = y + dy[i];
  20. int nx = x + dx[i];
  21. if(ny < 0 || ny >= n || nx < 0 || nx >= m || visited[ny][nx]) continue;
  22. visited[ny][nx] = 1;
  23. if(a[ny][nx] == '1'){
  24. v.push_back({ny, nx});
  25. continue;
  26. }
  27. dfs(ny, nx);
  28. }
  29. }
  30. int main(){
  31. ios_base::sync_with_stdio(false);
  32. cin.tie(NULL); cout.tie(NULL);
  33. cin >> n >> m >> x1 >> y1 >> x2 >> y2;
  34. for(int i = 0; i < n; i++){
  35. for(int j = 0; j < m; j++){
  36. cin >> a[i][j];
  37. }
  38. }
  39.  
  40. while(true){
  41. fill(&visited[0][0], &visited[0][0] + 301 * 301, 0);
  42. v.clear();
  43.  
  44. visited[y1 - 1][x1 - 1] = 1;
  45. dfs(y1 - 1, x1 - 1);
  46.  
  47. if(isEnd) break;
  48.  
  49. for(auto b : v) a[b.first][b.second] = '0';
  50. cnt++;
  51. }
  52. cout << cnt << '\n';
  53. }
Success #stdin #stdout 0s 5308KB
stdin
5 7
3 4 1 2
1#10111
1101001
001*111
1101111
0011001
stdout
3