yoyoboyss

Untitled

Feb 12th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. const int NMAX=103;
  7.  
  8. int dy[4]={0, 0, -1, 1};
  9. int dx[4]={-1, 1, 0, 0};
  10.  
  11. ifstream fin("taxe2.in");
  12. ofstream fout("taxe2.out");
  13. queue <pair <int , int> > Q;
  14.  
  15. int H[NMAX][NMAX], Lee[NMAX][NMAX];
  16. int S, n;
  17.  
  18. bool OK(int i, int j){
  19. if(i<0 || j<0 || i>=n || j>=n)
  20. return false;
  21. }
  22.  
  23. int read(){
  24. fin >> S >> n;
  25. for(int i=0; i<n; i++)
  26. for(int j=0; j<n; j++)
  27. fin >> H[i][j];
  28. }
  29.  
  30. void afisare_matrice(int matrice[NMAX][NMAX]){
  31. for(int i=0; i<n; i++){
  32. for(int j=0; j<n; j++){
  33. cout << matrice[i][j];
  34. if(matrice[i][j]>=10 || matrice[i][j]<0)
  35. cout << ' ';
  36. else
  37. cout << " ";
  38. }
  39. cout << endl;
  40. }
  41. }
  42.  
  43. int lee(){
  44. int i, j, i_1, j_1;
  45. Lee[0][0]=H[0][0];
  46. Q.push(make_pair(0, 0));
  47. while(!Q.empty()){
  48. i = Q.front().first;
  49. j = Q.front().second;
  50. for(int d=0; d<4; d++){
  51. i_1 = i + dy[d];
  52. j_1 = j + dx[d];
  53. if(OK(i_1, j_1)){
  54. if(!Lee[i_1][j_1]){
  55. Lee[i_1][j_1]=Lee[i][j]+H[i_1][j_1];
  56. Q.push(make_pair(i_1, j_1));
  57. } else if(Lee[i][j]+H[i_1][j_1]<Lee[i_1][j_1]){
  58. Lee[i_1][j_1]=Lee[i][j]+H[i_1][j_1];
  59. Q.push(make_pair(i_1, j_1));
  60. }
  61. }
  62. }
  63. Q.pop();
  64. }
  65. }
  66.  
  67. int main() {
  68. read();
  69. lee();
  70. afisare_matrice(Lee);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment