UVa 657 – The die is cast

題還是蠻簡單的,不過因為將main()函數的reture 0寫到了while裡,導致WA了好幾遍,當時老糾結了,怎麼看怎麼對,但就是WA,哎,太粗心了~~~兩次DFS,對骰子進行一次DFS,再對骰子裡面的數字進行一次DFS ~代碼如下:#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<string&

UVa 784 – Maze Exploration

DFS ~代碼如下:#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<string>using namespace std;char a[30+5][80+5];int vis[30+5][80+5];void DFS(int x,int y){

UVa 10004 – Bicoloring

一個圖的題,但也可以找規律來做。兩種顏色圖一個圖,要求相鄰兩點顏色不能相同,顯然,以三角形環為例往外推,只要是形成奇數多邊形的環,就不可能實現相鄰兩點的顏色不同,因此只要以每個點為基準,找出所有形成的環,只要存在奇數環就說明不能被塗色,否則就可以~~~代碼如下:#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#inclu

UVa 301 – Transportation

枚舉子集+回溯。枚舉子集時,需要在每一次遞迴的過程中進行條件判斷,而不能按一般情況放到最後進行判斷。代碼如下:#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;int n, m, num, _max, cal[8];struct point{ int start,

UVa 331 – Mapping the Swaps

回溯即可(注意題意是只能交換相鄰的兩個元素)。代碼如下:#include <iostream>#include <algorithm>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;int a[6], b[6], cct, fct, n;void dfs(int cur, int fi){

UVa 193 – Graph Coloring

回溯,枚舉所有點的黑白情況,然後判斷可行性。代碼如下:#include <iostream>#include <algorithm>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;int map[102][102], node[102], save[101], n, k, cct;int

UVa 10400 – Game Show Math

DFS+剪枝。因為題目中要求每一步運算的範圍都不會超過-32000~32000這個區間,因此可以將每一步運算的狀態存在一個vis數組裡,只要重複則不必要再往下計算,這樣就可以將3的99次方的運算量降為99*4*64000。代碼如下:#include <iostream>#include <algorithm>#include <cstring>#include <cstdlib>#include <cstdio>#include

UVa 131 – The Psychic Poker Player

一手牌(規則請見:http://dpk888.com/school/discussions/%E4%B8%80%E6%89%8B%E7%89%8C%E7%9A%84%E7%89%8C%E5%9E%8B-poker-hands#per_page=10),枚舉 + 類比,關鍵是明白其桌子上的牌相當於一個棧,只有丟掉手裡相應數量的牌才可以從棧中依次抽出相應數量的牌來維持手裡始終有五張牌,依此枚舉。代碼如下:#include <iostream>#include

UVa 10267-Graphical Editor

簡單的類比+DFS題,注意初始化時像素為大寫字母‘O’,而不是數字‘0’。還有就是注意需要保證 x1 < x2, y1 <  y2。代碼如下:#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;int m, n, vis[252][252];char a[252]

UVa 216 – Getting in Line

乍一看,像最小產生樹,實際不用,因為資料量較小(最大8台電腦),所以直接類比全排列算最小值就可以。代碼如下:#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;struct point{ int x, y;} po[10];int n;double save_d[10]

UVa 110 – Meta-Loopless Sorts

實際就是求一個全排列,因為給出的數字最大是8,所以最多有8!種情況。根據給出的範例我們可以將其看成向已給出的排列中倒敘插入新元素(其實就是插空),如:給出n=3,則最開始為 a,然後將b插入最後為ab,再插入c,為abc,c依次向前插入acb,cab,然後遞迴返回上一層,b向前移順序變為ba,繼而相繼產生bac,bca,cba。代碼如下:#include <iostream>#include <cstring>#include

UVa 10716 – Evil Straw Warts Live

題意:判斷一個字串是否能轉化成迴文串,如果可以就統計轉化成迴文串所需的最少交換次數。貪心,思路就是先將串兩端固定(對應字母移至對應位置),依次向中間夾逼,因為只有依次將字母先放到兩端,才可以減少剩下字母交換的次數,先把哪對字母放到兩端都可以,因為可以證明出無論移動哪對字母只要是每次都將其放到兩端其最後交換次數是一樣的。由題意不難判斷如果各個字母出現次數均為偶數,那他一定可以轉換成迴文串,如果有奇數次的字母出現,其有且至多出現一個這樣的字母。如果這個字母出現的次數為一次,我們可以在交換時“忽略”它

UVa 639 – Don’t Get Rooked

類似“八皇后問題”,但加了“牆”的概念,如果有牆相隔,則同一行牆兩邊可同時存在兩個元素。枚舉子集+判斷可行性,在枚舉子集時,可以進行適當剪枝,如此可以減少很多不必要的枚舉,比如:當n == 4時,其最大的的“車”的位置為8個,則我們可以加一個計數器,最多將子集的枚舉量設為8,超過8則不再往子集中加入元素。代碼如下:#include <iostream>#include <cstring>#include <cstdlib>#include

UVa 146 – ID Codes

算是簡單題吧,實質上就是求比給定字串稍微大一點的字串,只要從最後開始依次以最後一個元素為基準,向前與第一個比自身小的元素交換,然後把這個元素後邊的元素按增序排列就可以了。代碼如下:#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<string>using namespace std;int

UVa 11205 – The broken pedometer

求可區分出所有行的最小列數,用位向量法產生子集,然後枚舉最小子集。最需要注意的是其0與1之間可能不止一個空格,開始用char型數組 scanf("%c%c", &a[i][j], &c); 讀取的,因為這個WA了好幾次,如果直接用int型數組求則可以省去很多麻煩。代碼如下:#include <iostream>#include <algorithm>#include <cstdlib>#include

UVa 10098 – Generating Fast

簡單題,和146 - ID Codes有幾分相似,在此基礎上做就可以了 ~代碼如下:#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<string>using namespace std;int c_cmp(const void *a,const void *b){ return

UVa 572 – Oil Deposits

圖題裡最簡單的一個吧 ,DFS~代碼如下:#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<string>using namespace std;char a[100+2][100+2];int flag[100+2][100+2];void DFS(int x,int y){ if(!

UVa 10167 – Birthday Cake

暴力,最多一百個點,將所有點帶入方程,使得大於零與小於零的個數相同(直線兩邊點的個數相同)。剛開始只判斷了一邊==num ,WA了好幾邊,換成兩邊都==num後,AC了 ~~代碼如下:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>void makeit(int *a, int *b, int num){ int i, j; for(i = -10

UVa 439 – Knight Moves

第一個BFS,費了點時間~~題目本身就是象棋裡馬走“日”,每次共8種走法,記錄走過的格子,有限次數內能搜到目標格 ~~代碼如下:#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<string>using namespace std;int

UVa 699 – The Falling Leaves

實質上是一個二叉樹的DFS,先左子樹,再右子樹,因為題目說最大寬度為80,所以開一個80的數組,從中間(40)開始,往裡遞迴。以第二組資料為例,8 2 9 -1 -1 6 5 -1 -1 12 -1-1 3 7 -1 -1 -1開始a[40]=8,其左子樹 2 (a[39]=2),右子樹 3 (a[41]=3);2的左子樹為 9

總頁數: 61357 1 .... 13816 13817 13818 13819 13820 .... 61357 Go to: 前往

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.