请问能用搜索做这道题吗?我用搜索超时了
查看原帖
请问能用搜索做这道题吗?我用搜索超时了
1170547
LgFvm9353楼主2024/1/24 20:43
#include<stdio.h>
int n, m, mx, my, ans;
int next[2][2] = { {1,0},{0,1} };
int nn[9][2] = { {0,0},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-1,2},{-2,1} };
void dfs(int x, int y) {
    int tx, ty;
    if (x == n && y == m) ans++;
    for (int i = 0; i <= 1; i++) {
        tx = x + next[i][0];
        ty = y + next[i][1];
        if (tx<0 || ty<0 || tx>n || ty>m) {
            continue;
        }
        //int flag = 0;
        //for (int j = 0; j <= 8; j++) {
        //    if (tx == mx + nn[j][0] && ty == my + nn[j][1]) {
        //        flag = 1;
        //        break;
        //    }
        //}
        if (tx == mx && ty == my || tx == mx + 1 && ty == my + 2 || tx == mx + 2 && ty == my + 1 || tx == mx + 2 && ty == my - 1 || tx == mx + 1 && ty == my - 2 || tx == mx - 1 && ty == my - 2 || tx == mx - 2 && ty == my - 1 || tx == mx - 1 && ty == my + 2 || tx == mx - 2 && ty == my + 1) {
            continue;
        }
       // if (flag == 1) continue;
        dfs(tx, ty);
    }
    return;
}
int main() {
    scanf("%d %d %d %d", &n, &m, &mx, &my);
    n += 2; m += 2; mx += 2; my += 2;
    dfs(2, 2);
    printf("%d\n", ans);

    return 0;
}
2024/1/24 20:43
加载中...