Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. // zadacha.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include <iostream>
  5. #include <queue>
  6. #include <stack>
  7.  
  8. typedef long long ll;
  9.  
  10. using namespace std;
  11.  
  12. struct point {
  13.     int x, y;
  14. };
  15.  
  16. char f[12][12];
  17.  
  18. #define INRECT( a , b ) ( a >= 0 && b >= 0 && a < w && b < h && f[a][b] == '.' )
  19.  
  20. ll GetSec( int w , int h ) {
  21.     stack<int> price[12][12];
  22.  
  23.     for (int i = 0; i < w; i++) {
  24.         for (int j = 0; j < h; j++) {
  25.             if (f[i][j] != 'D') continue;
  26.             point start = { i , j };
  27.            
  28.             queue<point> q;
  29.             bool used[12*12];
  30.             memset( used, 0, sizeof(used) );
  31.             q.push( start );
  32.             used[12 * j + i] = true;
  33.             price[i][j].push( -1 );
  34.  
  35.             while( !q.empty() ) {
  36.                 point p = q.front();
  37.                 q.pop();
  38.  
  39.                 int nx = p.x + 1;
  40.                 int ny = p.y;
  41.                 if (INRECT( nx , ny )) {
  42.                     if (!used[12 * ny + nx]) {
  43.                         used[12 * ny + nx] = true;
  44.                         point np = { nx , ny };
  45.                         q.push( np );
  46.                         price[nx][ny].push( price[p.x][p.y].top() + 1 );
  47.                     }
  48.                 }
  49.                
  50.                 nx = p.x;
  51.                 ny = p.y + 1;
  52.                 if (INRECT( nx, ny )) {
  53.                     if (!used[12 * ny + nx]) {
  54.                         used[12 * ny + nx] = true;
  55.                         point np = { nx , ny };
  56.                         q.push( np );
  57.                         price[nx][ny].push( price[p.x][p.y].top() + 1 );
  58.                     }
  59.                 }
  60.                 nx = p.x - 1;
  61.                 ny = p.y;
  62.                 if (INRECT( nx, ny )) {
  63.                     if (!used[12 * ny + nx]) {
  64.                         used[12 * ny + nx] = true;
  65.                         point np = { nx , ny };
  66.                         q.push( np );
  67.                         price[nx][ny].push( price[p.x][p.y].top() + 1 );
  68.                     }
  69.                 }
  70.                 nx = p.x;
  71.                 ny = p.y - 1;
  72.                 if (INRECT( nx, ny )) {
  73.                     if (!used[12 * ny + nx]) {
  74.                         used[12 * ny + nx] = true;
  75.                         point np = { nx , ny };
  76.                         q.push( np );
  77.                         price[nx][ny].push( price[p.x][p.y].top() + 1 );
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.     }
  83.  
  84.     while ( 1 ) {
  85.         for (int i = 0; i < w; i++) {
  86.             for (int j = 0; j < h; j++) {
  87.                 if (f[i][j] != 'x') {
  88.                     if (price[i][j].empty()) {
  89.                         goto END;
  90.                     }
  91.                     cout << price[i][j].top() << " ";
  92.                     price[i][j].pop();
  93.                 }
  94.                 else {
  95.                     cout << " 0" << endl;
  96.                 }
  97.             }
  98.             cout << endl;
  99.         }
  100.     }
  101. END:;
  102.     return 0;
  103. }
  104.  
  105. int main()
  106. {
  107.     ll n;
  108.  
  109.     cin >> n;
  110.  
  111.     for (ll i = 0; i < n; i++) {
  112.         int w, h;
  113.        
  114.         cin >> w >> h;
  115.    
  116.         for (int x = 0; x < w; x++) {
  117.             for (int y = 0; y < h; y++) {
  118.                 cin >> f[x][y];
  119.             }
  120.         }
  121.        
  122.         ll sec = GetSec( w , h );
  123.     }
  124.  
  125.     system( "PAUSE" );
  126.  
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement