日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ1069 The Bermuda Triangle DFS

發布時間:2023/12/16 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ1069 The Bermuda Triangle DFS 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這個坐標系建的真牛。。。。。。比賽碰到這種題目,必跪。。。。。。

見坐標是給每一個單位三角分配,不是一個點分配。

有兩種建立方法,我用了夾角120方法。。。。。都一樣的。。。

The Bermuda Triangle
Time Limit:?2000MS?Memory Limit:?32768K
Total Submissions:?1096?Accepted:?509

Description

People in the hidden region of the Bermuda Triangle make everything they need in triangular shapes. One day, someone decided to break the rule and bake a hexagonally shaped cake. But as usual, he has to serve the cake in triangular pieces. The pieces are equilateral triangles but in different sizes for different people. He can use as many triangles as needed to cut the cake into pieces, such that nothing remains from the cake. For example, the following figure shows one way that a hexagon with side 9 can be cut into triangles with side 2 and 3. (The cake is cut along the thick lines, thin lines are drawn to show the sizes).?

Input is a hexagon and triangle types (specified by the length of their sides) and the goal is to decide if the hexagon can be completely divided by the given triangle types.

Input

The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. Each test case consists of a single line, containing s (1 <= s <= 25), the length of the hexagon's side, followed by n, the number of triangle types (1 <= n <= 10), followed by n integers representing the length of each triangle type's side (between 1 and 25, inclusive).

Output

There should be one output line per test case containing either YES or NO depending on whether the hexagon can be completely divided by the given triangle types.

Sample Input

3 5 2 2 3 7 2 3 2 13 2 2 3

Sample Output

NO NO YES



//此題難再見坐標,其他OK。。 //dfs用小正三角覆蓋大正三角,問能否完全覆蓋 //120坐標系建立 //畫圖自己研究。。。。。。、 //逐行DFS,填完一行在下一行。從下往上 從左往右 //一個三角一個三角覆蓋 //判斷能夠覆蓋的時候,小三角不行大三角一定不行 //背包優化。。這里沒用 , 還有一種剪枝也沒用 就是先判斷1/2 1/3 1/6的覆蓋。。。 //a[i]%a[j]==0就不要a[i]了; //注意如果sz%a[i]==0 那么直接可以了。。。。 #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cstdio>using namespace std;#define MAXN 110int sz,a[15],n,tp; int g[MAXN][MAXN];bool jud1() {for(int i=0;i<tp;i++)if(sz%a[i]==0) return true;return false; }//120度建立坐標 //橫向擴大2 縱向不便,具體理解自己畫圖 void init() {memset(g,0,sizeof(g));for(int i=1;i<=sz;i++)for(int j=1;j<=sz*2+2*i-1;j++)g[i][j]=1;for(int i=sz+1;i<=sz*2;i++)for(int j=(i-sz)*2;j<=sz*4;j++)g[i][j]=1;}bool judSZ(int x,int y,int size) {if(y+2*size-2>sz*4 || x+size-1>sz*2) return false;if(y%2==1) //倒三角 規律自己畫圖看注意列坐標{for(int i=0;i<size;i++)for(int j=0;j<2*i+1;j++)if(!g[x+i][y+j]) return false;}else //正三角{for(int i=0;i<size;i++)for(int j=2*i;j<2*size-1;j++)if(!g[x+i][y+j]) return false;}return true; }//覆蓋回復和上面檢查能否覆蓋一樣 void cover(int x,int y,int size) {if(y%2==1){for(int i=0;i<size;i++)for(int j=0;j<2*i+1;j++)g[x+i][y+j]=0;}else{for(int i=0;i<size;i++)for(int j=2*i;j<2*size-1;j++)g[x+i][y+j]=0;} }void remove(int x,int y,int size) {if (y%2==1){for(int i=0;i<size;i++)for(int j=0;j<2*i+1;j++)g[x+i][y+j]=1;}else{for(int i=0;i<size;i++)for(int j=2*i;j<2*size-1;j++)g[x+i][y+j]=1;} }bool dfs(int x,int y) {if(x>sz*2) return true;if(y>sz*4) return dfs(x+1,1);if(!g[x][y]){int j;for(j=y+1;y<=4*sz;y++)if(g[x][j]) break;return dfs(x,j);}for(int i=0;i<tp;i++){if(judSZ(x,y,a[i])){cover(x,y,a[i]);if(dfs(x,y+1)) return true;remove(x,y,a[i]);}elsebreak;}return false; }int main() {int cs;cin>>cs;while(cs--){cin>>sz;cin>>n;tp=0;for(int i=0;i<n;i++){scanf("%d",&a[tp++]);// if(a[tp]<=sz) tp++;}for(int i=0;i<tp;i++)for(int j=0;j<tp;j++){if(i==j) continue;if(a[i]%a[j]==0){swap(a[i],a[tp-1]);i--,tp--;break;}}sort(a,a+tp);if(jud1()){printf("YES\n");continue;}init();if(dfs(1,1)) printf("YES\n");else printf("NO\n");}return 0; }

總結

以上是生活随笔為你收集整理的POJ1069 The Bermuda Triangle DFS的全部內容,希望文章能夠幫你解決所遇到的問題。

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