#include <iostream>
#include <algorithm>
#include <cstring>
#include <limits.h>
#include <string>
using namespace std;
int arr[501][501];
int n, m, cnt = 0;
bool visit[501][501];
int aa = 0;
void go(int x, int y,int before) {
       if (x == n - 1 && y == m - 1) {
              cnt++;
              return;
       }
       if (before <= arr[x][y]) {
              return;
       }
       if (x > n - 1 || y > m - 1 || y<0) {
              return;
       }
       
       /*if (aa == 10) { 경로확인용 test 코드
              aa = 0;
              cout << endl;
       }
       cout << arr[x][y] << ' ';
       aa++;*/
       go(x + 1, y, arr[x][y]);
       go(x, y+1, arr[x][y]);
       go(x, y-1, arr[x][y]);
       go(x - 1, y, arr[x][y]);
}
int main(void)
{
       
       cin >> n >> m;
       for (int i = 0; i < n; ++i) {
              for (int j = 0; j < m; ++j) {
                     cin >> arr[i][j];
              }
       }
       go(0,0,INT_MAX);
       cout << cnt << endl;
       
       return 0;
}
 
완전탐색으로 구현 => 시간초과
Memoization 을 어떻게 해야할 지 모르겠다. 

dfs로 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <limits.h>
#include <string>

using namespace std;

int arr[501][501];
int n, m, cnt = 0;
int d[501][501];
int aa = 0;
int dx[4] = { 1,-1,0,0 };
int dy[4] = { 0,0,-1,1 };


int go(int x, int y) {
    if (x == n - 1 && y == m - 1) {
        return 1;
    }
    if (d[x][y] != -1) {
        return d[x][y];
    }

    int& ans = d[x][y]+=1;
    for (int k = 0; k < 4; ++k) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        if (0 <= nx && nx < n && 0 <= ny && ny < m) {
            if (arr[nx][ny] < arr[x][y]) {
                ans += go(nx, ny);
            }
        }
    }
    return ans;

}

int main(void)
{
    memset(d, -1, sizeof(d));
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> arr[i][j];
        }
    }
    
    cout << go(0, 0) << endl;
    
    return 0;
}




'알고리즘' 카테고리의 다른 글

[DP] 백준 1932 숫자삼각형  (0) 2018.04.29
[DP] 백준 14501 퇴사  (0) 2018.04.29
[DP] 백준 9251 LCS  (0) 2018.04.29
[DP] 백준 10942 팰린드롬?  (0) 2018.04.29
[DP] 백준 1890 점프, 3372 보드 점프 다시풀만한 것  (0) 2018.04.29

 
LCS
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main(void)
{
       int LCS_length = 0, max;
       int **table;
       string a, b;
       cin >> a >> b;
       a = "0" + a;
       b = "0" + b;
       int len1, len2;
       len1 = a.length();
       len2 = b.length();
       table = new int*[len2];
       for (int i = 0; i < len2; ++i) {
              table[i] = new int[len1];
       }
       for (int i = 0; i < len1; ++i) {
              table[0][i] = 0; //table에서 첫번째 열 초기화
       }
       for (int i = 1; i < len2; ++i) {
              max = 0;
              table[i][0] = 0;
              for (int j = 1; j < len1; ++j) {
                     if (a[j] == b[i]) {
                           max = table[i - 1][j - 1] + 1;
                           table[i][j] = max;
                     }
                     else {
                           if (table[i][j - 1] > table[i - 1][j])
                                  table[i][j] = table[i][j - 1];
                           else
                                  table[i][j] = table[i - 1][j];
                     }
              }
              if (LCS_length < max)
                     LCS_length = max;
       }
       cout << LCS_length << endl;
       return 0;
}

참고한 곳 

'알고리즘' 카테고리의 다른 글

[DP] 백준 14501 퇴사  (0) 2018.04.29
[DP] 백준 1520 내리막길  (0) 2018.04.29
[DP] 백준 10942 팰린드롬?  (0) 2018.04.29
[DP] 백준 1890 점프, 3372 보드 점프 다시풀만한 것  (0) 2018.04.29
[DP] 백준 2096 내려가기  (0) 2018.04.29

ios_base... 을 안해주면 시간초과가 났던 문제
처음에 for문으로 돌려도 시간초과 안날 것 같아서 해봤는데 시간초과가 났다. 
근데 DP로 짜니 for문보다 훨씬 빠르고 깔끔하게 짜졌다. 좀 익숙해 진 것 같다. 

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
int tb[2001];
int cache[2001][2001];
int go(int a, int b) {
       if (b - a<1) {
              return 1;
       }
       if (cache[a][b] != -1) {
              return cache[a][b];
       }
       int& ret = cache[a][b] = 0;
       if (tb[a] == tb[b]) {
              ret = go(a + 1, b - 1);
       }
       else {
              ret = 0;
       }
       return ret;
}
int main(void)
{
       ios_base::sync_with_stdio(false);
       cin.tie(NULL);
       memset(cache, -1, sizeof(cache));
       int n;
       cin >> n;
       for (int i = 0; i<n; ++i) {
              cin >> tb[i];
       }
       int t;
       cin >> t;
       while (t--) {
              int a, b;
              cin >> a >> b;
              cout << go(a - 1, b - 1) << '\n';
       }
       return 0;
}


+ Recent posts