日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

[ CodeVS冲杯之路 ] P1116

發布時間:2023/11/27 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [ CodeVS冲杯之路 ] P1116 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  不充錢,你怎么AC?

  題目:http://codevs.cn/problem/1116/

?

  數據很小,DFS可A,每層枚舉顏色,判斷相鄰的點是否有重復的顏色,記得回溯時把顏色染回0,即無顏色

  這里我使用了一個優化,在讀入的時候將相鄰的點壓入數組,這樣在判斷的時候時間就小于O(n)

  不過這個優化好像沒有不回溯的方法好,然而并沒有寫不回溯的

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 
 9 const int N=10;
10 int a[N],f[N][N],n,ans;
11 void dfs(int x)
12 {
13     if (x>n)
14         {
15             ans++;
16             return;
17         }
18     int i,j;
19     for (i=1;i<=4;i++)
20         {
21             for (j=1;j<=f[x][0];j++) if (a[f[x][j]]==i) break;
22             if (j<=f[x][0]) continue;
23             a[x]=i;
24             dfs(x+1);
25             a[x]=0;
26         }
27 }
28 int main()
29 {
30     int i,x,j;
31     scanf("%d",&n);
32     for (i=1;i<=n;i++)
33         for (j=1;j<=n;j++)
34             {
35                 scanf("%d",&x);
36                 if (x) f[i][++f[i][0]]=j;
37             }
38     dfs(1);
39     printf("%d\n",ans);
40     return 0;
41 }

?

轉載于:https://www.cnblogs.com/hadilo/p/5903621.html

總結

以上是生活随笔為你收集整理的[ CodeVS冲杯之路 ] P1116的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。