hust 1425【DFS】

來源:互聯網
上載者:User

http://acm.hust.edu.cn/thx/problem.php?id=1425

 

再次orz青蛙牛&菊神的神思路,已經把DFS理解得出神入化

思路:首先要知道

(x1,y1,z1)
(x2,y2,z2)
(x3,y3,z3)
的結果除以3模數,和
(x1%3,y1%3,z1%3)
(x2%3,y2%3,z2%3)
(x3%3,y3%3,z3%3)
的結果除以3模數
是一樣的,然後因為abc可能是負數,負數%3=負數或0。

用cnt數組記錄同類的有多少,然後DFS。。。額,比較難說清,看代碼吧。總之很神!

#include <iostream><br />#include <stdio.h><br />#include <string.h></p><p>using namespace std;<br />typedef long long LL;</p><p>int cnt[3][3][3];<br />int tmp[30];<br />int x[4],y[4],z[4];<br />LL cc[4];<br />LL res;</p><p>int cal(){<br /> return x[1]*y[2]*z[3]+x[2]*y[3]*z[1]+x[3]*y[1]*z[2]<br /> -x[3]*y[2]*z[1]-x[2]*y[1]*z[3]-x[1]*y[3]*z[2];<br />}</p><p>void DFS(int depth) {<br /> if (depth == 4) {<br /> if (cal()%3 == 0) {<br /> res = res + cc[1]*cc[2]*cc[3];<br /> }<br /> return ;<br /> }<br /> int i,j,k;<br /> for (i=0; i<=2; ++i){<br /> for (j = 0; j <= 2; ++j){<br /> for (k = 0; k <= 2 ; ++k){<br /> if (cnt[i][j][k]){<br /> cc[depth] = cnt[i][j][k];<br /> cnt[i][j][k]--;<br /> x[depth]=i;<br /> y[depth]=j;<br /> z[depth]=k;<br /> DFS(depth+1);<br /> cnt[i][j][k]++;<br /> }<br /> }<br /> }<br /> }<br />}</p><p>int main()<br />{<br /> //freopen("a.txt","r",stdin);<br /> int t,cas = 0;<br /> scanf ("%d", &t);<br /> while (t --) {<br /> int n;<br /> cas++;<br /> scanf ("%d", &n);<br /> memset(cnt, 0,sizeof (cnt));<br /> for (int i = 0; i < n; i ++) {<br /> int a, b, c;<br /> scanf ("%d%d%d", &a, &b, &c);<br /> cnt[(a % 3 + 3) % 3][(b % 3 + 3) % 3][(c % 3 + 3) % 3] ++; }<br /> res = 0;<br /> DFS(1);<br /> printf("Case #%d: ",cas);<br /> printf("%lld/n",res);<br /> }<br /> return 0;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.