Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // zadacha.cpp: определяет точку входа для консольного приложения.
- //
- #include <iostream>
- #include <queue>
- #include <stack>
- typedef long long ll;
- using namespace std;
- struct point {
- int x, y;
- };
- char f[12][12];
- #define INRECT( a , b ) ( a >= 0 && b >= 0 && a < w && b < h && f[a][b] == '.' )
- ll GetSec( int w , int h ) {
- stack<int> price[12][12];
- for (int i = 0; i < w; i++) {
- for (int j = 0; j < h; j++) {
- if (f[i][j] != 'D') continue;
- point start = { i , j };
- queue<point> q;
- bool used[12*12];
- memset( used, 0, sizeof(used) );
- q.push( start );
- used[12 * j + i] = true;
- price[i][j].push( -1 );
- while( !q.empty() ) {
- point p = q.front();
- q.pop();
- int nx = p.x + 1;
- int ny = p.y;
- if (INRECT( nx , ny )) {
- if (!used[12 * ny + nx]) {
- used[12 * ny + nx] = true;
- point np = { nx , ny };
- q.push( np );
- price[nx][ny].push( price[p.x][p.y].top() + 1 );
- }
- }
- nx = p.x;
- ny = p.y + 1;
- if (INRECT( nx, ny )) {
- if (!used[12 * ny + nx]) {
- used[12 * ny + nx] = true;
- point np = { nx , ny };
- q.push( np );
- price[nx][ny].push( price[p.x][p.y].top() + 1 );
- }
- }
- nx = p.x - 1;
- ny = p.y;
- if (INRECT( nx, ny )) {
- if (!used[12 * ny + nx]) {
- used[12 * ny + nx] = true;
- point np = { nx , ny };
- q.push( np );
- price[nx][ny].push( price[p.x][p.y].top() + 1 );
- }
- }
- nx = p.x;
- ny = p.y - 1;
- if (INRECT( nx, ny )) {
- if (!used[12 * ny + nx]) {
- used[12 * ny + nx] = true;
- point np = { nx , ny };
- q.push( np );
- price[nx][ny].push( price[p.x][p.y].top() + 1 );
- }
- }
- }
- }
- }
- while ( 1 ) {
- for (int i = 0; i < w; i++) {
- for (int j = 0; j < h; j++) {
- if (f[i][j] != 'x') {
- if (price[i][j].empty()) {
- goto END;
- }
- cout << price[i][j].top() << " ";
- price[i][j].pop();
- }
- else {
- cout << " 0" << endl;
- }
- }
- cout << endl;
- }
- }
- END:;
- return 0;
- }
- int main()
- {
- ll n;
- cin >> n;
- for (ll i = 0; i < n; i++) {
- int w, h;
- cin >> w >> h;
- for (int x = 0; x < w; x++) {
- for (int y = 0; y < h; y++) {
- cin >> f[x][y];
- }
- }
- ll sec = GetSec( w , h );
- }
- system( "PAUSE" );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement