Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define For(i, a, b) for(int i=a; i<b; i++)
- #define ffi For(i, 0, N)
- #define ffj For(j, 0, M)
- #define s <<" "<<
- #define w cout
- #define e endl
- #define pb push_back
- //500,000,000 operations
- //Global Variables
- int N, M, grid[100][100], sx, sy, ex, ey;
- char inp[100][100];
- int out = 100000;
- bool fs = false;
- queue<int> que1, que2;
- void search(int x, int y) {
- For (i, x+1, N) {
- if (grid[i][y] == -2) break;
- if (grid[i][y] == -1) {
- grid[i][y] = grid[x][y]+1;
- que1.push(i);
- que2.push(y);
- }
- }
- for (int i=x-1; i>=0; i--) {
- if (grid[i][y] == -2) break;
- if (grid[i][y] == -1) {
- grid[i][y] = grid[x][y]+1;
- que1.push(i);
- que2.push(y);
- }
- }
- For (i, y+1, M) {
- if (grid[x][i] == -2) break;
- if (grid[x][i] == -1) {
- grid[x][i] = grid[x][y]+1;
- que1.push(x);
- que2.push(i);
- }
- }
- for (int i=y-1; i>=0; i--) {
- if (grid[x][i] == -2) break;
- if (grid[x][i] == -1) {
- grid[x][i] = grid[x][y]+1;
- que1.push(x);
- que2.push(i);
- }
- }
- }
- void s1(int x, int y) {
- For (i, x+1, N) {
- if (grid[i][y] == -2) break;
- if (grid[i][y] == -1) {
- grid[i][y] = grid[x][y];
- que1.push(i);
- que2.push(y);
- }
- }
- for (int i=x-1; i>=0; i--) {
- if (grid[i][y] == -2) break;
- if (grid[i][y] == -1) {
- grid[i][y] = grid[x][y];
- que1.push(i);
- que2.push(y);
- }
- }
- For (i, y+1, M) {
- if (grid[x][i] == -2) break;
- if (grid[x][i] == -1) {
- grid[x][i] = grid[x][y];
- que1.push(x);
- que2.push(i);
- }
- }
- for (int i=y-1; i>=0; i--) {
- if (grid[x][i] == -2) break;
- if (grid[x][i] == -1) {
- grid[x][i] = grid[x][y];
- que1.push(x);
- que2.push(i);
- }
- }
- while (!que1.empty()) {
- int a = que1.front();
- int b = que2.front();
- que1.pop();
- que2.pop();
- search(a, b);
- }
- }
- int main() {
- //ifstream cin ("test.in");
- //ofstream cout ("test.out");
- cin >> M >> N;
- ffi {
- ffj {
- grid[i][j] = -1;
- cin >> inp[i][j];
- if (inp[i][j] == 'C') {
- if (fs) {ex = i; ey = j;}
- else {fs = true; sx = i; sy = j;}
- }
- if (inp[i][j] == '*') grid[i][j] = -2;
- }
- }
- grid[sx][sy] = 0;
- s1(sx, sy);
- w<<grid[ex][ey]<<e;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement