Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define nmax 505
- using namespace std;
- const short dx[ ] = {-1,0,1,0};
- const short dy[ ] = {0,1,0,-1};
- deque <pair <int, int > > d;
- int n, G, q;
- int a[ nmax ][ nmax ], v[ nmax ][ nmax ];
- ifstream f("rover.in");
- ofstream g("rover.out");
- void citire(){
- f >> q;
- if (q == 1)
- f >> G;
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= n; j++)
- f >> a[ i ][ j ];
- }
- bool bun( int x, int y ){
- return (x >= 0) && (x <= n) && (y >= 0) && (y <= n);
- }
- void q1(){
- memset(v, -1, sizeof( v ));
- d.push_back(make_pair(1, 1));
- v[ 1 ][ 1 ] = 0;
- while (!d.empty()){
- int x = d.front().first, y = d.front().second;
- d.pop_front();
- for (int i = 0; i < 4; i++){
- int nx = x + dx[ i ];
- int ny = y + dy[ i ];
- if (bun( nx, ny ) && v[ nx ][ ny ] == -1)
- if (a[ nx ][ ny ] < G){
- v[ nx ][ ny ] = v[ x ][ y ] + 1;
- d.push_back(make_pair( nx, ny ));
- }
- else{
- v[ nx ][ ny ] = v[ x ][ y ];
- d.push_front(make_pair( nx, ny ));
- }
- }
- }
- g << v[ n ][ n ];
- }
- int main(){
- citire();
- //if (q == 1)
- q1();
- // else
- // q2();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement