From 710546d6933899a6afd72707f2e90809d7093fb1 Mon Sep 17 00:00:00 2001 From: FFjet Date: Sat, 13 Jul 2019 20:48:23 +0800 Subject: [PATCH] Update backtracking.md MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 修改了一下码风 --- docs/search/backtracking.md | 140 +++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 68 deletions(-) diff --git a/docs/search/backtracking.md b/docs/search/backtracking.md index d106b845..97a606cd 100644 --- a/docs/search/backtracking.md +++ b/docs/search/backtracking.md @@ -23,37 +23,41 @@ ```cpp #include using namespace std; -int ans[14],check[3][28]={0},sum=0,n; +int ans[14], check[3][28] = {0}, sum = 0, n; void eq(int line) { - if(line>n) - { - sum++; - if(sum>3) return; - else - { - for(int i=1;i<=n;i++) printf("%d ",ans[i]); - printf("\n"); - return; - } - } - for(int i=1;i<=n;i++) - { - if((!check[0][i])&&(!check[1][line+i])&&(!check[2][line-i+n])) - { - ans[line]=i; - check[0][i]=1; check[1][line+i]=1; check[2][line-i+n]=1; - eq(line+1); - check[0][i]=0; check[1][line+i]=0; check[2][line-i+n]=0; - } - } + if(line > n) + { + sum++; + if(sum > 3) return; + else + { + for(int i = 1; i <= n; i++) printf("%d ", ans[i]); + printf("\n"); + return; + } + } + for(int i = 1; i <= n; i++) + { + if((!check[0][i]) && (!check[1][line + i]) && (!check[2][line - i + n])) + { + ans[line] = i; + check[0][i] = 1; + check[1][line + i] = 1; + check[2][line - i + n] = 1; + eq(line + 1); + check[0][i] = 0; + check[1][line + i] = 0; + check[2][line - i + n] = 0; + } + } } int main() { - scanf("%d",&n); - eq(1); - printf("%d",sum); - return 0; + scanf("%d", &n); + eq(1); + printf("%d", sum); + return 0; } ``` @@ -63,58 +67,58 @@ int main() ```cpp using namespace std; -int n,m,k,x,y,a,b,ans; -int dx[4] = {0,0,1,-1},dy[4] = {1,-1,0,0}; +int n, m, k, x, y, a, b, ans; +int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}; bool vis[6][6]; -struct oo{ - int x,y,used[6][6]; +struct oo +{ + int x, y, used[6][6]; }; oo sa; void bfs() { - queue q; - sa.x = x; - sa.y = y; - sa.used[x][y] = 1; - q.push(sa); - while(!q.empty()) - { - oo now = q.front(); - q.pop(); - for(int i = 0;i < 4; i++) - { - int sx = now.x + dx[i]; - int sy = now.y + dy[i]; - if( now.used[sx][sy] || vis[sx][sy] || sx == 0 || sy == 0 || sx > n || sy > m) - continue; - if(sx == a && sy == b) - { - ans++; - continue; - } - sa.x = sx; - sa.y = sy; - memcpy(sa.used,now.used,sizeof(now.used)); - sa.used[sx][sy] = 1; - q.push(sa); - } - } + queue q; + sa.x = x; + sa.y = y; + sa.used[x][y] = 1; + q.push(sa); + while(!q.empty()) + { + oo now = q.front(); + q.pop(); + for(int i = 0; i < 4; i++) + { + int sx = now.x + dx[i]; + int sy = now.y + dy[i]; + if(now.used[sx][sy] || vis[sx][sy] || sx == 0 || sy == 0 || sx > n || sy > m) + continue; + if(sx == a && sy == b) + { + ans++; + continue; + } + sa.x = sx; + sa.y = sy; + memcpy(sa.used, now.used, sizeof(now.used)); + sa.used[sx][sy] = 1; + q.push(sa); + } + } } int main() { - scanf("%d%d%d",&n,&m,&k); - scanf("%d%d%d%d",&x,&y,&a,&b); - for(int i = 1,aa,bb;i <= k; i++) - { - scanf("%d%d",&aa,&bb); - vis[aa][bb] = 1; - } - bfs(); - printf("%d",ans); - return 0; + scanf("%d%d%d", &n, &m, &k); + scanf("%d%d%d%d", &x, &y, &a, &b); + for(int i = 1, aa, bb; i <= k; i++) + { + scanf("%d%d", &aa, &bb); + vis[aa][bb] = 1; + } + bfs(); + printf("%d", ans); + return 0; } ``` - -- 2.11.0