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 && x == x2){
  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. cin >> n >> m >> x1 >> y1 >> x2 >> y2;
  32. for(int i = 0; i < n; i++){
  33. for(int j = 0; j < m; j++){
  34. cin >> a[i][j];
  35. }
  36. }
  37.  
  38. while(true){
  39. fill(&visited[0][0], &visited[0][0] + 301 * 301, 0);
  40. v.clear();
  41.  
  42. visited[y1][x1] = 1;
  43. dfs(y1, x1);
  44.  
  45. if(isEnd) break;
  46.  
  47. for(auto b : v) a[b.first][b.second] = '0';
  48. cnt++;
  49. }
  50. cout << cnt << '\n';
  51. }
Success #stdin #stdout 0.01s 5320KB
stdin
3 3
2 2 1 1
#00
0*0
000
stdout
1