Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "iostream"
- #include "queue"
- using namespace std;
- struct momento
- {
- int x;
- int y;
- int p;
- };
- queue< momento> c;
- bool visitado[51][51];
- char mapa[51][51];
- int mover[4][2] = {{1,0}, {0,-1}, {0,1}, {-1,0}};
- int n;
- int main()
- {
- cin.sync_with_stdio(0);
- cin.tie(0);
- cin >> n;
- for(int i = 0; i < n; i++)
- for(int j = 0; j < n; j++)
- cin >> mapa[i][j];
- c.push((momento){0, 0, 1});
- while(not c.empty())
- {
- momento actual = c.front();
- visitado[actual.x][actual.y] = true;
- for(int i = 0; i < n; i++)
- {
- for(int j = 0; j < n; j++)
- cout << visitado[i][j]?"1 ": "0 ";
- cout << endl;
- }
- cout << endl;
- if(actual.x == n - 1 and actual.y == n - 1)
- {
- cout << actual.p << "\n";
- return 0;
- }
- for(int i = 0; i < 4; i ++)
- {
- int posx = actual.x + mover[0][i];
- int posy = actual.y + mover[1][i];
- if(posx >= 0 and posx < n and
- posy >= 0 and posy < n and
- not visitado[posx][posy] and mapa[posx][posy] != '#')
- {
- //cout << "49 " << posx << " " << posy << "\t" <<actual.p << endl;
- if(mapa[posx][posy] >= 48 and mapa[posx][posy] <= 57)
- {
- //cout << char(mapa[posx][posy]) << endl;
- int numero = int(mapa[posx][posy]) - 48;
- //cout << numero << endl;
- if(actual.p % numero - 1 == 0)
- c.push((momento){actual.x, actual.y, actual.p + 1});
- else
- c.push((momento){posx, posy, actual.p + 1});
- }
- else
- c.push((momento){posx, posy, actual.p + 1});
- }
- }
- c.pop();
- }
- cout << "-1\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement