ABOUT ME

문제의 간단한 해법과 함께 어떤 방식으로 접근했는지, 그리고 문제의 해법을 찾는데 결정적이었던 깨달음은 무엇이었는지

Today
Yesterday
Total
  • 백준 1937번 욕심쟁이 판다
    PS 2020. 2. 24. 23:52

    https://www.acmicpc.net/source/17861879

     

    로그인

     

    www.acmicpc.net

     

     

     

     

     

     

    <접근방법>

    dp문제 입니다.

    종만북 dp 반쯤 공부하다가 말았는데, 쉬운 난이도는 커버가 되는 느낌이네요

     

     


     

     

     

    <코드>

    #include <iostream>
    #include <algorithm>
    #include <memory.h>
    using namespace std;
    
    
    int n, map[501][501];
    int cache[501][501];
    int dy[4] = { 1, -1, 0, 0 };
    int dx[4] = { 0, 0, 1, -1 };
    
    int dfs(int y, int x) {
    	int &res = cache[y][x];
    	if (res != -1) {
    		return res;
    	}
    
    	res = 1;
    	for (int i = 0; i < 4; i++) {
    		int ty = y + dy[i];
    		int tx = x + dx[i];
    
    		if (ty < 0 || ty >= n || tx < 0 || tx >= n) continue;
    		if (map[y][x] >= map[ty][tx]) continue;
    
    		res = max(res, dfs(ty, tx) + 1);
    	}
    	return res;
    }
    
    
    int main() {
    	memset(cache, -1, sizeof(cache));
    
    	cin >> n;
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
    			cin >> map[i][j];
    		}
    	}
    
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
    			if (cache[i][j] == -1) {
    				cache[i][j] = dfs(i, j);
    			}
    		}
    	}
    
    	int ans = 0;
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
    			ans = max(ans, cache[i][j]);
    		}
    	}
    	cout << ans << '\n';
    	return 0;
    }

     

     


     

     

    <느낀 점>

     

     

     

     

     

     

    반응형

    'PS' 카테고리의 다른 글

    백준 9376번 탈옥  (0) 2020.02.27
    백준 2931번 가스관  (0) 2020.02.27
    백준 2638번 치즈  (0) 2020.02.24
    백준 2169번 로봇 조종하기  (0) 2020.02.24
    백준 1194번 달이 차오른다, 가자  (0) 2020.02.24
Designed by Tistory.