Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // prof. Mircea Lupse-Turpan - Liceul Teoretic Grigore Moisil Timisoara - 100 puncte
- #include <fstream>
- #include <deque>
- #include <cstring>
- using namespace std;
- ifstream fin("rover.in");
- ofstream fout("rover.out");
- const int NMax = 505;
- const int oo = 10000;
- int A[NMax][NMax],DP[NMax][NMax];
- int V,N,G;
- int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
- deque <pair <int,int> > Q;
- void Read()
- {
- fin >> V >> N;
- if(V == 1) fin >> G;
- for(int i = 1; i <= N; ++i)
- for(int j = 1; j <= N; ++j)
- fin >> A[i][j];
- }
- inline bool Inside(int x, int y)
- {
- return ( (x >= 1) && (x <= N) && (y >= 1) && (y <= N) );
- }
- void Solve1()
- {
- memset(DP,-1,sizeof(DP));
- Q.push_back(make_pair(1,1));
- DP[1][1] = 0;
- while(!Q.empty())
- {
- int X = Q.front().first, Y = Q.front().second;
- Q.pop_front();
- for(int k = 0; k < 4; k++)
- {
- int NewX = X + dx[k], NewY = Y + dy[k];
- if(Inside(NewX,NewY) && DP[NewX][NewY] == -1)
- {
- if(A[NewX][NewY] < G)
- {
- DP[NewX][NewY] = DP[X][Y] + 1;
- Q.push_back(make_pair(NewX,NewY));
- }
- else
- {
- DP[NewX][NewY] = DP[X][Y];
- Q.push_front(make_pair(NewX,NewY));
- }
- }
- }
- }
- fout << DP[N][N] << "\n";
- }
- bool Lee(int Value)
- {
- memset(DP,-1,sizeof(DP));
- Q.push_back(make_pair(1,1));
- DP[1][1] = 0;
- while(!Q.empty())
- {
- int X = Q.front().first, Y = Q.front().second;
- Q.pop_front();
- for(int k = 0; k < 4; k++)
- {
- int NewX = X + dx[k], NewY = Y + dy[k];
- if(Inside(NewX,NewY) && DP[NewX][NewY] == -1)
- {
- if(A[NewX][NewY] >= Value)
- {
- DP[NewX][NewY] = DP[X][Y] + 1;
- Q.push_back(make_pair(NewX,NewY));
- }
- }
- }
- }
- return (DP[N][N] != -1);
- }
- void Solve2()
- {
- int Left = 1, Right = oo, Sol = -1;
- while(Left <= Right)
- {
- int Mid = (Left + Right) / 2;
- if(Lee(Mid))
- {
- Sol = Mid;
- Left = Mid + 1;
- }
- else
- Right = Mid - 1;
- }
- fout << Sol << "\n";
- }
- int main()
- {
- Read();
- if(V == 1)
- Solve1();
- else
- Solve2();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement