#2327586

Solution for 1144: Curling 2.0 by godofd

Source Code Status Test Cases
    Policy: public     Reviewed: 12    
00.01 sec    3044 KB    67 lines     1297 bytes    2017-05-20 03:28
#include <iostream>

int stage[20][20];
int w, h;

bool out( int x, int y ) {
	return (x < 0 || y < 0 || x >= w || y >= h);
}

int search( int x, int y, int n ) 
{
	if ( n == 10 ) return 0xfffff;
	int dx[4] = {1, -1, 0, 0};
	int dy[4] = {0, 0, 1, -1};

	int min = 0xfffff;
	for ( int i = 0; i < 4; ++i ) {
		int px = x, py = y;
		if ( out(px+dx[i], py+dy[i]) || stage[py+dy[i]][px+dx[i]] == 1 ) continue;
		for ( int j = 0; (px >= 0 && py >= 0 && px < w && py < h && stage[py][px] == 0); ++j ) {
			px += dx[i]; py += dy[i];
		}
		if (px < 0 || py < 0 || px >= w || py >= h) {
			continue;
		} else {
			if ( stage[py][px] == 3 ) return n+1;
			else {
				stage[py][px] = 0;
				int a = search( px-dx[i], py-dy[i], n+1 );
				min = (min > a) ? a : min;
				stage[py][px] = 1;
			}
		}
	}
	return min;
}

int main(void)
{
	while ( true ) {
		std::cin >> w >> h;
		if ( w == 0 && h == 0 ) break;
		for ( int i = 0; i < 20; ++i ) {
			for ( int j = 0; j < 20; ++j ) {
				stage[i][j] = 0;
			}
		}

		int x, y;
		for ( int i = 0; i < h; ++i ) {
			for ( int j = 0;j < w; ++j ) {
				std::cin >> stage[i][j];
				if ( stage[i][j] == 2 ) {
					stage[i][j] = 0;
					x = j; y = i;
				}
			}
		}

		int res = search(x, y, 0);
		std::cout << ((res == 0xfffff) ? -1 : res) << std::endl;
	}

	return 0;
}


Compile Error Logs:
You are not authorized to see the message.

Status
Judge: 1/1 C++ CPU: 00.01 sec Memory: 3044 KB Length: 1297 B 2017-05-20 03:28 2017-05-20 03:28
Results for testcases
Case # Verdict CPU Time Memory In Out Case Name
Case #1: : Accepted 00.01 sec 3044 KB
< prev | / | next >  
 
Judge Input #  ( | ) Judge Output #  ( | )


Comments
 
 Under Construction.
 
Categories
 
 
Free Tags