Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <math.h>
- #include <algorithm>
- #include <stack>
- #include <vector>
- #include <string.h>
- #include <string>
- #include <queue>
- #include <deque>
- using namespace std;
- struct point {
- int x,y;
- };
- int main()
- {
- freopen("input.txt","r",stdin);
- //freopen("output.txt","w",stdout);
- int n,m,i,j;
- string s;
- cin>>n>>m;
- int map[n][m];
- int used[n][m];
- memset(used,0,sizeof(used));
- int dx[] = {0,-1,0,1};
- int dy[] = {-1,0,1,0};
- point start, end;
- for (i=0; i<n; ++i) {
- cin>>s;
- for (j=0; j<m; ++j) {
- if (s[j]-48 == 0 || s[j]-48 == 1) map[i][j] = s[j]-48;
- else if (s[j] == '#') {
- start.x = i;
- start.y = j;
- used[i][j] = 1;
- } else if (s[j] == '@') {
- end.x = i;
- end.y = j;
- map[i][j] = 0;
- }
- }
- }
- queue <point> q;
- q.push(start);
- while (!q.empty()) {
- point current = q.front();
- q.pop();
- int x1 = current.x;
- int y1 = current.y;
- for (i=0; i<4; ++i) {
- if (x1+dx[i] >= 0 && x1+dx[i] < n && y1+dy[i] >= 0 && y1+dy[i] < m && !used[x1+dx[i]][y1+dy[i]] && !map[x1+dx[i]][y1+dy[i]] ) {
- used[x1+dx[i]][y1+dy[i]] = 1;
- point next;
- next.x = x1+dx[i];
- next.y = y1+dy[i];
- q.push(next);
- }
- }
- }
- if (!used[end.x][end.y]) cout<<"NO"; else cout<<"YES";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment