hdu-1198 Farm Irrigation---并查集+模拟(附测试数据)
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1198
题目大意:
有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇水,问需要打多少口井。
例如下面这个土地块
ADC
FJK
IHE
then the water pipes are distributed like
解题思路:
首先模拟A-K的四个不同的方向,用了一个数组表示从A-K的四个方向是否有水管
然后枚举每个点的四个方向上可到达的地方,如果连通就合并并查集,最后根节点的数目就是连通块的数目(DFS也可以做,判断连通块即可)
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 10000 + 10; 5 int p[maxn]; 6 char Map[55][55]; 7 int dir[4][2] = {0,-1,-1,0,0,1,1,0};//分别是左(0),上(1),右(2),下(3) 8 int G[11][4]={ 9 1,1,0,0, 10 0,1,1,0, 11 1,0,0,1, 12 0,0,1,1, 13 0,1,0,1, 14 1,0,1,0, 15 1,1,1,0, 16 1,1,0,1, 17 1,0,1,1, 18 0,1,1,1, 19 1,1,1,1, 20 }; 21 void init() 22 { 23 for(int i = 0; i < maxn; i++)p[i] = i; 24 } 25 int Find(int x) 26 { 27 return x == p[x] ? x : p[x] = Find(p[x]); 28 } 29 void Union(int x, int y) 30 { 31 x = Find(x); 32 y = Find(y); 33 if(x != y) 34 { 35 p[x] = y; 36 } 37 } 38 int main() 39 { 40 int n, m; 41 freopen("out.txt", "w", stdout); 42 while(cin >> n >> m && ( n + m > 0)) 43 { 44 init(); 45 for(int i = 0; i < n; i++) 46 { 47 cin >> Map[i]; 48 } 49 for(int i = 0; i < n; i++) 50 { 51 for(int j = 0; j < m; j++) 52 { 53 int c = Map[i][j] - 'A'; 54 for(int k = 0; k < 4; k++)//枚举四个方向 55 { 56 if(!G[c][k])continue;//判断该方向是否存在水管 57 int x = i + dir[k][0]; 58 int y = j + dir[k][1]; 59 if(x < 0 || x >= n || y < 0 || y >= m)continue; 60 int d = Map[x][y] - 'A'; 61 if(!G[d][(k + 2) % 4])continue;//这里是判断(x,y)处是否有和k方向相对的方向的水管 62 Union(i * m + j, x * m + y);//这里是编号i*m+j,一开始写错成i*n+j 63 } 64 } 65 } 66 int flag = 0; 67 for(int i = 0; i < n * m; i++) 68 { 69 if(p[i] == i)flag++;//根节点的数目也就是连通块的数目 70 } 71 cout<<flag<<endl; 72 } 73 return 0; 74 }
测试数据:
1 41 17 2 JBHFFKBAHHFIGHDHJ 3 CHHIKGHIFGHCBJKKC 4 EJHFEHFGHAFAFEFBD 5 KGAAGHIHKDFCKJKDK 6 BBGIFFCEGGFGFABEF 7 GFHEAIEEADDFIEHIC 8 AIHHEBFCHJAKDIEBG 9 CJCDGFJFHEGBKHJKH 10 AHCAAKHKBBKJJJGGB 11 HJJHFIBDAKEHEFHGD 12 CJJICAAJJEJIHJDEK 13 FKKIKJBKGKKJEEFIC 14 IIGJJIDFHKFDCKJDK 15 KBKFHKHDCJIDJBFDF 16 KBHBFJEIFFKFGCAIG 17 CKJEBHICKEGABEIHC 18 EJECEHIDDKJAIDEAG 19 KGIJAAJDHABDEIGCF 20 FIFJECCEKDAAKCACG 21 HIAFBCCDFIIKCAJBB 22 AAKGGBBDHAHJIIIEB 23 FCBIFKACGFEHIECII 24 IFBEKFGKFGADIHICH 25 HIJHFFEIHDGKEAKIE 26 GHCHDKBIECFJEFKHA 27 KHCHCDAGJDJEAKJJD 28 DKIKAJCCACJBCEDKD 29 IGHKIGDHBBFGJGKJK 30 DFGDIAJJHIIGCIFCC 31 AIGJJKKJJDJEEJFAE 32 CIEFBGGCBDFAFFEBK 33 EEAIDJEEEBDGKCFBG 34 DFJFACCJIGJHBDAGK 35 CEJFDBHCGDGHADAKB 36 IAJBKKHDEEAIJICBA 37 CBKFKEBJFJGJBDECH 38 EIFHJCBCBJKKIDDJA 39 GIABAIIDHEBIHGIDF 40 FFHFCFJAIFFEIJEJG 41 KAFFDFEIECBEFKCEK 42 ICKGEAFCJFIEHBHCG 43 9 20 44 GKDKJKAGKAICDIJB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BECJEHJHEC 92 JDHHHDECGCIJDFCDFGHCDJECCKG 93 DFEEGCJKHHCCCFEKKEIEEJIAKEA 94 BDIGGFKCHDKJAHCCIGBFIKAAIEC 95 KBECAFKFJBICDIEKEBIAGDCKFAH 96 FCJJEHCJCKFHIHFEFKCEJGDDIEH 97 BACDHJDAAIDABGFDKGEEBKKEKKJ 98 JHFHCGCBDBFIIFFAHFAFJGGAIFB 99 DHEDBHBFCFGEKGGDEFJFAHKDBBD 100 EGDDJIFEHIBDAIBIEHHFKDEIDAB 101 BGCCKBFBCBKKIFCAIJIECHICHEB 102 KCFKGABEKEFJFGBKGEAEFCJFIGH 103 BHFCEEJKBKGDBBJEEHKBFHDBGFF 104 CFHEDFEDJFKJECIAKBIEJAKHABD 105 DFEAFCEFEBHBEAKIDFDKEJAICCD 106 EJGDIHDHIBBEDFHKJEIFCCDHACC 107 GJGAIDBJDCGKKKHBAIDAAEACEDA 108 JCHFFAHCKGFFBCIFKGCAJAJKBBH 109 GHACAHIDBBIBCAAEHJBHGJGGIHI 110 KECKGDJGAIDIIDBJJFDKJBIHIFA 111 EGJKFCJAHHIIBJHIADJBAHHCDAH 112 AGDKCHCCECHHCAKCACHFJHIFJEA 113 41 8 114 GJAGCCED 115 DAKIBIAH 116 FAEFHFKA 117 KCCEDIIJ 118 ECAIDGJE 119 JJBAJDFI 120 HFIBJEJG 121 KEIGFDIC 122 CBGFFCKK 123 ICAGAFGH 124 HCEKFBBE 125 DFDDACDC 126 BEADDBKF 127 CAKBCEID 128 IHKJFIHD 129 KBIHCCHK 130 BKBDBCGG 131 EHFKJKJC 132 IHEAKAKH 133 DDHKKFBD 134 BHGKBCEI 135 EIJEICIH 136 HFHGGKJF 137 KJBJCHKC 138 ADBEGCDH 139 KIADDADJ 140 KJBEEBII 141 JFHKGBHK 142 CHJGKJJB 143 GEDECEED 144 BCJDIGBK 145 DIGDHFIE 146 ADIEFJGH 147 BAKACBDH 148 HBDBEJGD 149 BJICHIAH 150 ADIJEKID 151 HBEAHCBA 152 HIHCAKEK 153 CJHKCGBC 154 BKEEEKCJ 155 10 24 156 AGEEBDBKHKECEFBBCDIBHDDD 157 IGFGGKFEFHBFHCAHIBJEEDBG 158 BAJHKKEFEDKCIJIBECDBDBKH 159 JHHDCHJDHDIGHJKFHEHIGHKA 160 IEHCGKGJFFBEJDCHGEDIBDFH 161 CDHHBAGABFBDAFCJIBJDDKCC 162 CIJIFHFGHCCEECBGHEBCGDCI 163 FBCFIIJFHBJFKGJHAKBHGJIK 164 DGEFKHGJJCEFFKHKGGBCKIIH 165 IFGJCDGCDHIHBEDCJECHJBBJ 166 19 22 167 KKBFAGDHJEEGHFIDKFBBBB 168 BKIIBJIKJFJHJJDBDBJIHI 169 HCBGEDAEHHFCFJJAIDEKIG 170 JEDFBDGDHKDBIHEJCCFAKJ 171 CFJAAFIEKGKHCCAIHEBKBH 172 CBCEIEHBGIAKHAFHEAAHKK 173 ECDHIEHCBGAFIECFHCEBFG 174 BGBBFJJIJKKDCCKGBJJABH 175 AGBHCKJBDHBIBBHHIGGEGC 176 HJKDFKAKKHGJKKEHJCIGIB 177 DAFAEEFGDGKBFHJKHEIBBK 178 FBGBEEDDAEHHIIIHHFHBKJ 179 IJCEDACCJAFEGEEFHBKIEH 180 ADKEFBAFDDDCACIAIJCEIB 181 HBKGDDBBFIGHKEDGHECGJI 182 FDBACAADDKADFCFIHAIDKH 183 FBIFJBDCJGJAIBBKFBDFJH 184 KAEKBAJBGBAHDEFHBBHKJI 185 HJGJBJHBKBJHAFBACEBJDF 186 33 35 187 FBDBDHKDJBEGJIJHKDJFAHCKCBEDDDICECG 188 DKKAGKFHKIJCAHBDFDFBDKDEIEHFDIEABCG 189 GGHKFIFFKGEAKEBKHDFHIADCHGABEDAHJIE 190 KDHJFKFEKHIAJKJIFJKBGFECGJFHCEIECBB 191 BAGGKCKGBBKIECEIBCCAGEFIHKICKDHFKCG 192 FJICKFJGDAAAGCGJEIIHFCIHDJJCBCHIFAH 193 CEKGCHK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CCABIBIKG 222 KGJIBGACAJCCGKJHBJGBFBBDEBDKI 223 ICAHBGBBEFKFEEKACBIFIFGHICHCA 224 DHKCBJGFEFJHAFKKEKKBGJIGCGDEB 225 AFFDEKFBKBDIGGKAGIHIBGGKDABBK 226 JFIACGCFDIJHHGIDJKCFBEEEKAEIH 227 GIDICCGFFDFHIFBBDJECFHKGIHGCE 228 CEBABFIJDCIFHFKBAIJJGGCFKBEKC 229 FHIEKJKICFJEIKEGKIBGEAJADIHIF 230 EBKBHIIFEBBAGIBHIAJKBFJDDCFEG 231 CADAICKIJBHHEFIKIJHDEBBAEFGJI 232 JGEBHCDGIJDIKFCJBIHFHCFKBCGJK 233 DFJKCFABBIFFECHHGCKIHHKFJHBDJ 234 ACGCHJDKJGJFIGKFBIGKDBDBCKCBK 235 GKJHCIJGFHDHJAGACHFIFIAJBFGFK 236 FGJEAFIGBHGFBJBIJFJAHDDGCHIBK 237 FIFIEGHJIKFHJACAJKKCEFIGDHJGE 238 FBHDFKGDJJIKHKDCIJDIBFJHCFJAK 239 GGBDICEDIAEBFJIIIDHBKKBHHFKEB 240 JACKFBIHDCIGDBEGCDJFCKBDECACF 241 KHFIJBHKCCFCKECAJBKHHJACKEIGD 242 KFEAFBEIJEJHGGDAEIDJHAAHJEDFK 243 IGJEIAIJJKDKDCDDADJEGDCGCFDGE 244 GCIFJKCKHICIJKADADEGGFDCJFCDI 245 CEAHIBIDAEIACICKKHBJFGJGCECJE 246 HBKGFGCABDABCEKJCICFJDDJBJACI 247 JHGDJAKCHHEADHEJAJHABDCDJHBJI 248 EHJKCIEKACKJDJCAABGEAFJDKAAGC 249 DDKKJHDDDGJIADDDGEKEEJAJDEGDD 250 18 48 251 KAFKGFKCFABFGCHBGEAHBCBBDHKBBDHIJEJIIACHDCBICDAH 252 AHIEDHBIEGIIKBAEHJJFJDBGBIKKHBAFDADCAIEEJFGCAKEK 253 KCDDJCDEBGHGDCFFKIIEBEJHDDAHCIDGADGHAAIFDHACJCFH 254 DFJHDJCEAGGEAICHEHGDKFCIGKDKEFCFFFIJKHGGECGIKKKA 255 AJACFFJJFCDGDIGAGCHJGKAHGEFIECACKIBGAAIBHDHIEGAF 256 GJIBDFIBHAGAFIDGKFGHCJAJICDJIBJEAJDAEBKGCFIJDEGC 257 HBABAJCHGFKGIBGBIEJDEHJAKFFHFBBHHHAIJIEDGKKBDKIJ 258 KIKJDKBIGCJAAIJHDAGFBEEKEFKGDHJDABCIGGBKDEFJDCKB 259 FDFJADGHAFFEFKIDKJBGIEHDCDGKJBICHCIAKDGBAKGCFKDI 260 IHJHAKDHGEJHHGGFEIHIFBEGHAJKGAEJDCAKHJKABJKGBBKB 261 GHBAHCIFFJGFBCACABIDFKGHHDJDKGEEIAIKKEGFHGFEBBCD 262 HIBADGAAHJAIAGGKCDHIBAAEKKCAHGEDDKEHHFFJGIJAHICE 263 CFGCAIBCDDECDJHKCJGJDKCBHDAHIKDJKAJJDADCFEKJKAFA 264 GCDEFHEJAIAJDGFDDADJGEIAJCKBICDGABKEDAFFGBIIHDBE 265 AIFDACKBJHBEEGIJDFBCJEKFBHICGGBFJCHIBJCIJIJCHAEF 266 KBEFKDCIEEDEBKIIBGDHGJFHGKCHJGCJFIGIDACBIGCJHKEK 267 CIICHEEIBGHHFCICKGECHJFBFKHBAKEGBCKCHKHKCKFDHJDG 268 GIKABBAIKJJDEECKBEECEHBHECDEGDICCGAGCCHCJAADBIFK
答案:
1 166 2 52 3 106 4 256 5 83 6 63 7 115 8 249 9 205 10 212
越努力,越幸运