ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 5650. [모의 SW 역량테스트] 핀볼 게임
    SWexpertAcademy 2019. 7. 25. 05:01
    #include<cstdio>
    #include<iostream>
    #include<vector>
    #include<utility>
    #include<cstring>
    
    
    using namespace std;
    
    int tc;
    int map[102][102];
    int n;
    int ix, iy;
    vector<pair<int, int>> hole[5];
    
    void initial() {
    	for (int i = 0; i <= 100; i++) {
    		for (int j = 0; j <= 100; j++) {
    			map[i][j] = 0;
    		}
    	}
    	for (int i = 0; i < 5; i++)
    		hole[i].clear();
    	
    }
    
    int play(int x, int y, int dir, int z, bool h) {
    	if (dir == 1) {//up
    		while (x >= 0) {
    			x--; 
    			//cout << "up " << x << " " << y << endl;
    			int worm = map[x][y] - 6;
    			if (h == true) {
    				if (x == ix && y == iy) {
    					return z;
    				}
    			}
    			if (map[x][y] == -1) {
    				return z;
    			}
    			if (map[x][y] == 1 || map[x][y] == 4 || map[x][y] == 5) {
    				return play(x, y, 2, z + 1, true);
    			}
    			if (x == 0) {
    				return play(x , y, 2, z+1, true);
    			}
    			if (map[x][y] == 2) {
    				return play(x, y, 3, z + 1, true);
    			}
    			if (map[x][y] == 3) {
    				return play(x, y, 4, z + 1, true);
    			}
    			if (map[x][y] > 5) {
    				for (int i = 0; i < hole[worm].size(); i++)
    				{
    					if (x != hole[worm][i].first && y != hole[worm][i].second)
    					{
    						x = hole[worm][i].first; y = hole[worm][i].second;
    						return play(x, y, 1, z, true);
    					}
    
    				}
    			}
    		}
    	}
    	if (dir == 2) {
    		while (x <= n + 1) {
    			x++; 
    			//cout << "down " << x << " " << y << endl;
    			int worm = map[x][y] - 6;
    			if (h == true) {
    				if (x == ix && y == iy) {
    					return z;
    				}
    			}
    			if (map[x][y] == -1) {
    				return z;
    			}
    			if (map[x][y] == 2 || map[x][y] == 3 || map[x][y] == 5) {
    				return play(x, y, 1, z + 1, true);
    			}
    			if (x == n + 1) {
    				return play(x , y, 1, z+1, true);
    			}
    			if (map[x][y] == 1) {
    				return play(x, y, 3, z + 1, true);
    			}
    			if (map[x][y] == 4) {
    				return play(x, y, 4, z + 1, true);
    			}
    			if (map[x][y] > 5) {
    				for (int i = 0; i < hole[worm].size(); i++)
    				{
    					if (x != hole[worm][i].first && y != hole[worm][i].second)
    					{
    						x = hole[worm][i].first; y = hole[worm][i].second;
    						return play(x, y, 2, z, true);
    					}
    				}
    			}
    		}
    	}
    	if (dir == 3) {
    		while (y <= n + 1) {
    			y++;
    			//cout << "right " << x << " " << y << endl;
    			int worm = map[x][y] - 6;
    			if (h == true) {
    				if (x == ix && y == iy) {
    					return z;
    				}
    			}
    			if(map[x][y] == -1){
    				return z;
    		}
    			if (map[x][y] == 1 || map[x][y] == 2 || map[x][y] == 5) {
    				return play(x, y,4, z + 1, true);
    			}
    			if (y == n + 1)
    				return play(x, y ,4, z, true);
    			if (map[x][y] == 3) {
    				return play(x, y, 2, z + 1, true);
    			}
    			if (map[x][y] == 4) {
    				return play(x, y, 1, z + 1, true);
    			}
    			if (map[x][y] > 5) {
    				for (int i = 0; i < hole[worm].size(); i++)
    				{
    					if (x != hole[worm][i].first && y != hole[worm][i].second)
    					{
    						x = hole[worm][i].first; y = hole[worm][i].second;
    						return play(x, y, 3, z, true);
    					}
    
    				}
    			}
    		}
    	}
    	if (dir == 4) {
    		while (y >= 0) {
    			y--;
    			//cout << "left " << x << " " << y << endl;
    			int worm = map[x][y] - 6;
    			if (h == true) {
    				if (x == ix && y == iy) {
    					return z;
    				}
    			}
    			if (map[x][y] == -1)
    				return z;
    			if (map[x][y] == 3 || map[x][y] == 4 || map[x][y] == 5)
    				return play(x, y, 3, z + 1, true);
    			if (y == 0) {
    				return play(x, y , 3, z, true);
    			}
    			if (map[x][y] == 2) {
    				return play(x, y, 2, z + 1, true);
    			}
    			if (map[x][y] == 1) {
    				return play(x, y, 1, z + 1, true);
    			}
    			if (map[x][y] > 5) {
    				for (int i = 0; i < hole[worm].size(); i++)
    				{
    					if (x != hole[worm][i].first && y != hole[worm][i].second)
    					{
    						x = hole[worm][i].first; y = hole[worm][i].second;
    						return play(x, y, 4, z, true);
    					}
    
    				}
    			}
    		}
    	}
    
    }
    
    
    int main() {
    	scanf("%d", &tc);
    	for (int tn = 1; tn <= tc; tn++) {
    		initial();
    		scanf("%d", &n);
    		vector<pair<int, int>> start;
    
    		for (int i = 1; i<=n ; i++) {
    			for (int j = 1; j <= n; j++) {
    				scanf("%d", &map[i][j]);
    				if (map[i][j] == 0)
    					start.push_back(make_pair(i, j));
    				if (map[i][j] >= 6) {
    					hole[map[i][j] - 6].push_back(make_pair(i, j));
    				}
    
    			}
    		}
    
    		int max = 0;
    		for (int i = 0; i < start.size(); i++) {
    			ix = start[i].first; iy = start[i].second;
    			
    			for (int j = 1; j <= 4; j++)
    			{
    				int c = ix; int cc = iy;
    				int q = play(c, cc, j, 0,false);
    				
    				if (q > max)
    					max = q;
    			}
    		}
    		
    		
    		
    
    		printf("#%d %d\n", tn, max);
    	}
    	return 0;
    }

    처음에 재귀로 풀었을때다, 지역 스택이 과도하게쌓여서 런타임 에러가 나는듯..

    테스트 케이스는 잘 맞는다 시간도 준수한편, 런타임 에러떄문에 그냥 반복문만썼다.

     

    #include<cstdio>
    #include<iostream>
    #include<vector>
    #include<utility>
    #include<cstring>
    
    
    using namespace std;
    
    int tc;
    int map[102][102];
    int n;
    
    int dx[] = { -1,1,0,0 };//위 아래 왼 오른
    int dy[] = { 0,0,1,-1 };
    vector<pair<int, int>> hole[5];
    
    void initial() {
    	for (int i = 0; i <= 100; i++) {
    		for (int j = 0; j <= 100; j++) {
    			map[i][j] = 0;
    		}
    	}
    	for (int i = 0; i < 5; i++)
    		hole[i].clear();
    	
    }
    
    int play(int x, int y, int dir) {
    	int cnt = 0;
    	int ix = x;
    	int iy = y;
    	int nx = x + dx[dir];
    	int ny = y + dy[dir];
    	int d = dir;
    	while (true) {
    	
    		if (nx < 1 || ny < 1 || nx > n || ny > n) {
    			int ori;
    			if (d == 0) ori = 1;
    			else if (d == 1) ori = 0;
    			else if (d == 2) ori = 3;
    			else  ori = 2;
    			nx = nx + dx[ori];
    			ny = ny + dy[ori];
    			d = ori;
    			cnt++;
    			continue;
    		}
    		if (map[nx][ny] == -1 || (nx == ix && ny == iy)) {
    			return cnt;
    		}
    
    		if (map[nx][ny] == 0) {
    			nx = nx+ dx[d];
    			ny = ny+  dy[d];
    		}
    		else if (1 <= map[nx][ny] && map[nx][ny] <= 5) {//0 위 1 아래 2 오른 3 왼쪽
    			int ori;
    			if (map[nx][ny] == 1) {
    				if (d == 0) ori = 1;
    				else if (d == 1) ori = 2;
    				else if (d == 2) ori = 3;
    				else ori = 0;
    			}
    			if (map[nx][ny] == 2) {
    				if (d == 0) ori = 2;//위로가면 오른쪽
    				else if (d == 1) ori = 0;//아래로 가면 위쪽
    				else if (d == 2) ori = 3;//오른쪽으로 가면 왼족
    				else ori = 1;//왼쪽으로 가면 아래쪽
    			}
    			if (map[nx][ny] == 3) {
    				if (d == 0) ori = 3;//위쪽으로 가면 왼쪽
    				else if (d == 1) ori = 0;//아래쪽으로 가면 위쪽
    				else if (d == 2) ori = 1;//오른족으로 가면 아래쪽
    				else ori = 2;//왼쪽으로 가면 
    			}
    			if (map[nx][ny] == 4) {
    				if (d == 0) ori = 1;
    				else if (d == 1) ori = 3;
    				else if (d == 2) ori = 0;
    				else ori = 2;
    			}
    			if (map[nx][ny] == 5) {
    				if (d == 0) ori = 1;
    				else if (d == 1) ori = 0;
    				else if (d == 2) ori = 3;
    				else ori = 2;
    			}
    			nx += dx[ori];
    			ny += dy[ori];
    			d = ori;
    			cnt++;
    		}
    		if (6 <= map[nx][ny] && map[nx][ny] <= 10) {
    			int worm = map[nx][ny] - 6;
    			for (int i = 0; i < hole[worm].size(); i++) {
    				if (nx != hole[worm][i].first || ny != hole[worm][i].second) {
    					nx = hole[worm][i].first+dx[d]; ny = hole[worm][i].second+dy[d];
    					break;
    					
    				}
    			}
    		}
    	}
    }
    
    
    int main() {
    	scanf("%d", &tc);
    	for (int tn = 1; tn <= tc; tn++) {
    		initial();
    		scanf("%d", &n);
    		vector<pair<int, int>> start;
    
    		for (int i = 1; i<=n ; i++) {
    			for (int j = 1; j <= n; j++) {
    				scanf("%d", &map[i][j]);
    				if (map[i][j] == 0)
    					start.push_back(make_pair(i, j));
    				if (map[i][j] >= 6) {
    					hole[map[i][j] - 6].push_back(make_pair(i, j));
    				}
    
    			}
    		}
    
    		int max = 0;
    		for (int i = 0; i < start.size(); i++) {
    			
    			int x = start[i].first; int y = start[i].second;
    			for (int j = 0; j < 4; j++)
    			{
    				int q = play(x,y, j);
    				if (q > max)
    					max = q;
    			}
    			
    		}
    
    		printf("#%d %d\n", tn, max);
    	}
    	return 0;
    }

    새벽이라 계속 헷갈리고 해서 , 방향 주석으로 몇개 달아놨다, 정신멀쩡했으면 어렵지않았을텐데.. 짜증났던문제

    댓글

Designed by Tistory.