806 - Spatial Structures(DFS)

来源:互联网 发布:日本的男装品牌 知乎 编辑:程序博客网 时间:2024/04/29 04:45

没什么思路,难就难在麻烦,各种DFS,挺练基本功的...

  Problem  VerdictLangTimeBestRankSubmit Time | discuss806 - Spatial Structures AcceptedC++0.2720.045902 mins ago

中间被卡了一次,所以出了好多数据 QAQ,都填在这里吧

#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int maxn = 105;const int maxd = 100005;const int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};char mat[maxn][maxn];int vis[maxn][maxn];int base[15];int n,cnt,arr[maxd],Case = 1;struct Node{    Node *son[4];    int color;};int get_value(Node *node){    int c = node -> son[0] -> color;    for(int i = 1; i < 4; i++)        if(node -> son[i] -> color != c)            return -1;    return c;}Node *dfs(int x,int y,int len){    if(len == 1){        Node* node = (Node*)malloc(sizeof(Node));        node->color = mat[x][y] - '0';        return node;    }    Node* node = (Node*)malloc(sizeof(Node));    int l = len / 2;    node -> son[0] = dfs(x,y,l);    node -> son[1] = dfs(x,y + l,l);    node -> son[2] = dfs(x + l,y,l);    node -> son[3] = dfs(x + l,y + l,l);    node -> color  = get_value(node);    return node;}void dfs_print(Node *node,int value,int deep){    if(node -> color == 1){        arr[cnt++] = value;        return;    }    for(int i = 0; i < 4; i++)        if(node -> son[i] -> color)            dfs_print(node -> son[i],value + (i + 1) * base[deep],deep + 1);}void solve1(){    for(int i = 0; i < n; i++)        scanf("%s",mat[i]);    Node* root = dfs(0,0,n);    cnt = 0;    if(root -> color == 0);    else        dfs_print(root,0,0);    printf("Image %d\n",Case++);    sort(arr,arr + cnt);    for(int i = 0,j = 0; i < cnt; i++,j++){        if(j)            printf(" ");        printf("%d",arr[i]);        if(i == cnt - 1)            puts("");        else if((j + 1) % 12 == 0){            j = -1;            puts("");        }    }    printf("Total number of black nodes = %d\n",cnt);}void dfs_vis(int x,int y,int x1,int y1,int x2,int y2){    vis[x][y] = 1;    for(int i = 0; i < 4; i++){        int xx = x + dir[i][0];        int yy = y + dir[i][1];        if(xx >= x1 && xx < x2 && yy >= y1 && yy < y2 && !vis[xx][yy]){            dfs_vis(xx,yy,x1,y1,x2,y2);        }    }}void dfs2(int x,int y,int len,Node *node){    int ok = 0;    int l  = len / 2;    if(node -> son[0] != NULL){        ok = 1;        dfs2(x,y,l,node -> son[0]);    }    if(node -> son[1] != NULL){        ok = 1;        dfs2(x,y + l,l,node -> son[1]);    }    if(node -> son[2] != NULL){        ok = 1;        dfs2(x + l,y,l,node -> son[2]);    }    if(node -> son[3] != NULL){        ok = 1;        dfs2(x + l,y + l,l,node -> son[3]);    }    if(!ok){        dfs_vis(x,y,x,y,x + len,y + len);    }}void solve2(){    int x,ret = 0;    memset(vis,0,sizeof(vis));    Node *root = (Node*)malloc(sizeof(Node));    while(scanf("%d",&x)){        if(x < 0) break;        Node *node = root;        while(x){            int e = x % 5 - 1;            if(node -> son[e] == NULL)                node -> son[e] = (Node*)malloc(sizeof(Node));            node = node -> son[e];            x /= 5;        }        ret ++;    }    printf("Image %d\n",Case++);    if(ret == 0){        for(int i = 0; i < n; i++){            for(int j = 0; j < n; j++)                printf(".");            puts("");        }    }    else{        dfs2(0,0,n,root);        for(int i = 0; i < n; i++){            for(int j = 0; j < n; j++)                if(!vis[i][j])                    printf(".");                else                    printf("*");            puts("");        }    }}int main(){    base[0] = 1;    //freopen("in.txt","r",stdin);    for(int i = 1; i <= 10; i++)        base[i] = base[i - 1] * 5;    while(scanf("%d",&n) && n){        if(Case > 1)            puts("");        if(n > 0)            solve1();        else{            n = -n;            solve2();        }    }    return 0;}/*-89 14 17 22 23 44 63 69 88 94 113 -1-11 -1-10 -1-12 -1-13 -10*/

数据:

-10 -1-21 4 -1-46 8 12 14 16 18 22 24 -1-88 13 17 18 19 21 22 24 31 32 34 3637 39 59 64 66 73 81 82 84 86 87 8991 98 106 107 111 116 123 -1-1-180000000000000000000011110000111100011111001111110011110000111000-89 14 17 22 23 44 63 69 88 94 113 -120000-40 -1320101010101010101010101010101010110101010101010101010101010101010010101010101010101010101010101011010101010101010101010101010101001010101010101010101010101010101101010101010101010101010101010100101010101010101010101010101010110101010101010101010101010101010010101010101010101010101010101011010101010101010101010101010101001010101010101010101010101010101101010101010101010101010101010100101010101010101010101010101010110101010101010101010101010101010010101010101010101010101010101011010101010101010101010101010101001010101010101010101010101010101101010101010101010101010101010100101010101010101010101010101010110101010101010101010101010101010010101010101010101010101010101011010101010101010101010101010101001010101010101010101010101010101101010101010101010101010101010100101010101010101010101010101010110101010101010101010101010101010010101010101010101010101010101011010101010101010101010101010101001010101010101010101010101010101101010101010101010101010101010100101010101010101010101010101010110101010101010101010101010101010-321406 1407 1408 1409 1411 1412 1413 1414 1416 1417 1418 14191421 1422 1423 1424 1431 1432 1433 1434 1436 1437 1438 14391441 1442 1443 1444 1446 1447 1448 1449 1456 1457 1458 14591461 1462 1463 1464 1466 1467 1468 1469 1471 1472 1473 14741481 1482 1483 1484 1486 1487 1488 1489 1491 1492 1493 14941496 1497 1498 1499 1531 1532 1533 1534 1536 1537 1538 15391541 1542 1543 1544 1546 1547 1548 1549 1556 1557 1558 15591561 1562 1563 1564 1566 1567 1568 1569 1571 1572 1573 15741581 1582 1583 1584 1586 1587 1588 1589 1591 1592 1593 15941596 1597 1598 1599 1606 1607 1608 1609 1611 1612 1613 16141616 1617 1618 1619 1621 1622 1623 1624 1656 1657 1658 16591661 1662 1663 1664 1666 1667 1668 1669 1671 1672 1673 16741681 1682 1683 1684 1686 1687 1688 1689 1691 1692 1693 16941696 1697 1698 1699 1706 1707 1708 1709 1711 1712 1713 17141716 1717 1718 1719 1721 1722 1723 1724 1731 1732 1733 17341736 1737 1738 1739 1741 1742 1743 1744 1746 1747 1748 17491781 1782 1783 1784 1786 1787 1788 1789 1791 1792 1793 17941796 1797 1798 1799 1806 1807 1808 1809 1811 1812 1813 18141816 1817 1818 1819 1821 1822 1823 1824 1831 1832 1833 18341836 1837 1838 1839 1841 1842 1843 1844 1846 1847 1848 18491856 1857 1858 1859 1861 1862 1863 1864 1866 1867 1868 18691871 1872 1873 1874 2031 2032 2033 2034 2036 2037 2038 20392041 2042 2043 2044 2046 2047 2048 2049 2056 2057 2058 20592061 2062 2063 2064 2066 2067 2068 2069 2071 2072 2073 20742081 2082 2083 2084 2086 2087 2088 2089 2091 2092 2093 20942096 2097 2098 2099 2106 2107 2108 2109 2111 2112 2113 21142116 2117 2118 2119 2121 2122 2123 2124 2156 2157 2158 21592161 2162 2163 2164 2166 2167 2168 2169 2171 2172 2173 21742181 2182 2183 2184 2186 2187 2188 2189 2191 2192 2193 21942196 2197 2198 2199 2206 2207 2208 2209 2211 2212 2213 22142216 2217 2218 2219 2221 2222 2223 2224 2231 2232 2233 22342236 2237 2238 2239 2241 2242 2243 2244 2246 2247 2248 22492281 2282 2283 2284 2286 2287 2288 2289 2291 2292 2293 22942296 2297 2298 2299 2306 2307 2308 2309 2311 2312 2313 23142316 2317 2318 2319 2321 2322 2323 2324 2331 2332 2333 23342336 2337 2338 2339 2341 2342 2343 2344 2346 2347 2348 23492356 2357 2358 2359 2361 2362 2363 2364 2366 2367 2368 23692371 2372 2373 2374 2406 2407 2408 2409 2411 2412 2413 24142416 2417 2418 2419 2421 2422 2423 2424 2431 2432 2433 24342436 2437 2438 2439 2441 2442 2443 2444 2446 2447 2448 24492456 2457 2458 2459 2461 2462 2463 2464 2466 2467 2468 24692471 2472 2473 2474 2481 2482 2483 2484 2486 2487 2488 24892491 2492 2493 2494 2496 2497 2498 2499-1640101010101010101010101010101010101010101010101010101010101010101101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101011010101010101010101010101010101010101010101010101010101010101010010101010101010101010101010101010101010101010101010101010101010110101010101010101010101010101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101011010101010101010101010101010101010101010101010101010101010101010010101010101010101010101010101010101010101010101010101010101010110101010101010101010101010101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101011010101010101010101010101010101010101010101010101010101010101010010101010101010101010101010101010101010101010101010101010101010110101010101010101010101010101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101011010101010101010101010101010101010101010101010101010101010101010010101010101010101010101010101010101010101010101010101010101010110101010101010101010101010101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101011010101010101010101010101010101010101010101010101010101010101010010101010101010101010101010101010101010101010101010101010101010110101010101010101010101010101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101011010101010101010101010101010101010101010101010101010101010101010010101010101010101010101010101010101010101010101010101010101010110101010101010101010101010101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101011010101010101010101010101010101010101010101010101010101010101010010101010101010101010101010101010101010101010101010101010101010110101010101010101010101010101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101011010101010101010101010101010101010101010101010101010101010101010010101010101010101010101010101010101010101010101010101010101010110101010101010101010101010101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101011010101010101010101010101010101010101010101010101010101010101010010101010101010101010101010101010101010101010101010101010101010110101010101010101010101010101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101011010101010101010101010101010101010101010101010101010101010101010010101010101010101010101010101010101010101010101010101010101010110101010101010101010101010101010101010101010101010101010101010100101010101010101010101010101010101010101010101010101010101010101101010101010101010101010101010101010101010101010101010101010101001010101010101010101010101010101010101010101010101010101010101011010101010101010101010101010101010101010101010101010101010101010-8-10


0 0