Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define valoare 2000000000
- using namespace std;
- ifstream fin("rover.in");
- ofstream fout("rover.out");
- int n,g,a[503][503],b[503][503];
- queue< pair<int,int> >q;
- int dx[] = {1, 0, -1, 0};
- int dy[] = {0, 1, 0, -1};
- void Citire1()
- {
- int i,j;
- fin>>n>>g;
- for(i=1;i<=n;i++)
- for(j=1;j<=n;j++)
- fin>>a[i][j];
- }
- void Citire2()
- {
- int i,j;
- fin>>n;
- cout<<"n="<<n<<"\n";
- for(i=1;i<=n;i++)
- for(j=1;j<=n;j++)
- fin>>a[i][j];
- }
- void InitMatB()
- {
- int i,j;
- for(i=1;i<=n;i++)
- for(j=1;j<=n;j++)
- b[i][j] = -1;
- }
- bool inmatr(int i, int j)
- {
- if(!(1<=i && i<=n)) return false;
- if(!(1<=j && j<=n)) return false;
- return true;
- }
- void Cerinta1()
- {
- int i,j,i1,j1,periculos,k;
- InitMatB();
- q.push(make_pair(1,1));
- b[1][1] = 0;
- while(!q.empty())
- {
- i = q.front().first;
- j = q.front().second;
- q.pop();
- for(k=0;k<4;k++)
- {
- i1 = i + dx[k];
- j1 = j + dy[k];
- periculos = 0;
- if(a[i1][j1] < g)
- periculos = 1;
- if(inmatr(i1,j1) && (b[i1][j1] == -1 || (b[i1][j1] > b[i][j]+periculos)))
- {
- b[i1][j1] = b[i][j] + periculos;
- q.push(make_pair(i1,j1));
- }
- }
- }
- fout<<b[n][n]<<"\n";
- }
- void Lee2(int gr)
- {
- int i,j,i1,j1,k;
- InitMatB();
- cout<<gr<<"*\n";
- b[1][1] = 1;
- q.push(make_pair(1,1));
- while(!q.empty())
- {
- i = q.front().first;
- j = q.front().second;
- q.pop();
- for(k=0;k<4;k++)
- {
- i1 = i + dx[k];
- j1 = j + dy[k];
- if(inmatr(i1,j1) && a[i1][j1] >= gr && (b[i1][j1] == -1 || b[i1][j1] > b[i][j] + 1))
- {
- b[i1][j1] = b[i][j] + 1;
- q.push(make_pair(i1,j1));
- }
- }
- }
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=n;j++)
- fout<<b[i][j]<<"\t";
- fout<<"\n";
- }
- }
- void Cerinta2()
- {
- int gr,ok,x,sol;
- ok = 1;
- sol = 0;
- while(ok == 1)
- {
- Lee2(gr);
- x = b[n][n];
- gr++;
- if(x == -1) ok = 0;
- else sol = max(sol,x);
- }
- fout<<sol<<"\n";
- }
- int main()
- {
- int op;
- fin>>op;
- cout<<"op="<<op<<"\n";
- if(op == 1)
- {
- cout<<"Solutia1 trebuie facuta\n";
- Citire1();
- Cerinta1();
- }
- else
- {
- cout<<"Solutia2 trebuie facuta\n";
- Citire2();
- ///Cerinta2();
- Lee2(2);
- }
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement