2016 長春東北賽---Coconuts(離散化+DFS)

来源:http://www.cnblogs.com/chen9510/archive/2016/10/07/5936513.html
-Advertisement-
Play Games

題目鏈接 http://acm.hdu.edu.cn/showproblem.php?pid=5925 Problem Description TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams ...


題目鏈接

http://acm.hdu.edu.cn/showproblem.php?pid=5925

 

Problem Description TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams about eating. One day, TanBig dreams of a field of coconuts, and the field looks like a large chessboard which has R rows and C columns. In every cell of the field, there is one coconut. Unfortunately, some of the coconuts have gone bad. For sake of his health, TanBig will eat the coconuts following the rule that he can only eat good coconuts and can only eat a connected component of good coconuts one time(you can consider the bad coconuts as barriers, and the good coconuts are 4-connected, which means one coconut in cell (x, y) is connected to (x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1).

Now TanBig wants to know how many times he needs to eat all the good coconuts in the field, and how many coconuts he would eat each time(the area of each 4-connected component).   Input The first line contains apositiveinteger T(T10) which denotes the test cases. T test cases begin from the second line. In every test case, the first line contains two integers R and C, 0<R,C109 the second line contains an integer n, the number of bad coconuts, 0n200 from the third line, there comes n lines, each line contains two integers, xi and yi, which means in cell(xi,yi), there is a bad coconut.

It is guaranteed that in the input data, the first row and the last row will not have bad coconuts at the same time, the first column and the last column will not have bad coconuts at the same time.
  Output For each test case, output "Case #x:" in the first line, where x denotes the number of test case, one integer k in the second line, denoting the number of times TanBig needs, in the third line, k integers denoting the number of coconuts he would eat each time, you should output them in increasing order.   Sample Input 2 3 3 2 1 2 2 1 3 3 1 2 2   Sample Output Case #1: 2 1 6 Case #2: 1 8   Source 2016CCPC東北地區大學生程式設計競賽 - 重現賽  

 

Recommend wange2014   |   We have carefully selected several similar problems for you:  5932 5931 5930 5929 5928    題意:有一個R*C的棋盤方格,上面有n個障礙點,求這些障礙點將棋盤分割後的每一個區域的方格數,要求先輸出分成的區域數,然後從小到大輸出方格數;   思路:要求求出每一個區域的方格數,那麼可以DFS深搜求出來,但是R*C太大,會超時,所以必須要進行離散化,減小棋盤; 可以分別用兩個數組x[] y[] 存儲障礙點的橫縱坐標,然後從小到大排序,分別對x  y進行離散,這兩個離散過程相同,例如在對x進行離散的過程的中如果x[i]==x[i-1]  那麼離散到和x[i-1]同一行 ,如果x[i]==x[i-1]+1 那麼離散到x[i-1]對應行的下一行, 其餘情況離散到x[i-1]對應行的下一行的下一行;   代碼如下:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int N=405;
LL dx[N],dy[N];///表示每一格壓縮的長度;
bool v[N][N];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
LL r,c;
int n;

struct Node{
   long long v,p;
   int id;
}x[N],y[N];

bool cmp1(const Node s1,const Node s2){
   return s1.v<s2.v;
}
bool cmp2(const Node s1,const Node s2){
   return s1.id<s2.id;
}

LL dfs(int sx,int sy)
{
    LL sum=dx[sx]*dy[sy];
    v[sx][sy]=true;
    for(int i=0;i<4;i++){
        int nx=sx+dir[i][0];
        int ny=sy+dir[i][1];
        if(nx>0&&ny>0&&nx<=r&&ny<=c)
        if(!v[nx][ny]){
            sum+=dfs(nx,ny);
        }
    }
    return sum;
}

int main()
{
    int T,Case=1;
    cin>>T;
    while(T--)
    {
       printf("Case #%d:\n",Case++);
       scanf("%lld%lld%d",&r,&c,&n);
       for(int i=1;i<=n;i++)
       {
           scanf("%lld%lld",&x[i].v,&y[i].v);
           x[i].id=i;
           y[i].id=i;
       }
       sort(x+1,x+n+1,cmp1);
       sort(y+1,y+n+1,cmp1);
       x[n+1].v=r; y[n+1].v=c;
       x[n+1].id=y[n+1].id=n+1;
       x[0].v=y[0].v=1;
       x[0].id=y[0].id=0;

       int tot=1;
       dx[1]=1;
       for(int i=1;i<=n+1;i++)
       {
           if(x[i].v==x[i-1].v){
              x[i].p=tot;
           }
           else if(x[i].v==x[i-1].v+1){
              x[i].p=++tot;
              dx[tot]=1;
           }
           else {
              x[i].p=tot+2;
              dx[tot+2]=1;
              dx[tot+1]=x[i].v-x[i-1].v-1;
              tot+=2;
           }
       }
       r=tot;
       tot=1;
       dy[1]=1;
       for(int i=1;i<=n+1;i++)
       {
           if(y[i].v==y[i-1].v){
              y[i].p=tot;
           }
           else if(y[i].v==y[i-1].v+1){
              y[i].p=++tot;
              dy[tot]=1;
           }
           else {
              y[i].p=tot+2;
              dy[tot+2]=1;
              dy[tot+1]=y[i].v-y[i-1].v-1;
              tot+=2;
           }
       }
       c=tot;

       memset(v,0,sizeof(v));
       sort(x+1,x+n+1,cmp2);
       sort(y+1,y+n+1,cmp2);
       for(int i=1;i<=n;i++)
          v[x[i].p][y[i].p]=true;
       long long ans[N];
       tot=0;
       for(LL i=1;i<=r;i++)
        for(LL j=1;j<=c;j++)
           if(!v[i][j])
           ans[tot]=dfs(i,j),tot++;
        sort(ans,ans+tot);
        printf("%d\n",tot);
        for(int i=0;i<tot;i++)
            printf("%lld%c",ans[i],(i+1==tot)?'\n':' ');
    }
    return 0;
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本人最近想做個桌面應用,參考了一下時下流行的各個軟體,發現大部分軟體的標題欄都是自定義的,甚至沒有標題欄,整個視窗為一個整體。 從整體感來說,預設的標題欄非常的破壞軟體風格的一致性,尤其是設置背景圖時,標題欄的顏色會顯得很礙眼。 所以,找了一些大神分享的技術貼,也同時給大家分享下我的心得。 參考鏈接 ...
  • 這篇文章主要講述 IIS 8 部署免費 HTTPS 。 HTTPS 是互聯網 web 大勢所趨。TaSaid 最近把機房從香港遷移到青島,趁著這次機會,觀望並折騰了幾天,在遷移中順便完成了 HTTPS 的部署。 ...
  • 剛接到這樣的任務時,沒有感覺到任何壓力,不就是給移動端應用提供數據嗎?那邊發來參數,這邊處理數據,返回JSON。做網站開發時經常使用ajax請求後臺數據,不就是這麼回事嗎。於是,在確認完需求後就開始幹了,很快,進入聯調階段,這個時候各種問題來了,忙得不可開交。吃一塹,長一智,項目結束後總結了下,大致 ...
  • 虛方法和抽象方法都可以供派生類重寫,它們之間有什麼區別呢? 1. 虛方法必須有實現部分,併為派生類提供了覆蓋該方法的選項 抽象方法沒有提供實現部分,抽象方法是一種強制派生類覆蓋的方法,否則派生類將不能被實例化。如: //抽象方法 public abstract class Animal { publ ...
  • 一、獲取文件夾列表 /// /// 獲取文件夾下的文件列表 /// /// string Path:文件夾路徑(@"C:\") /// string SearchPattern:擴展名過濾(" .txt") /// bool SearchChild:為False不搜索子目錄,為True搜索子目錄 / ...
  • 在asp.net mvc項目里,用戶需要開拓幾個活動版面,並以側欄的方式呈現在首頁右側,幾個活動時間不一致,為避免瀏覽者在活動未開放之時進入未開放的服務頁面。因此不僅需要在活動代碼中加入限制功能,也需要在前臺取消不合時宜的頁面的展示。 ...
  • 第一部分:程式集(System.Reflection.Assembly) 1.獲取Assembly對象 方法1:調用Assembly的以下4個靜態方法Get...()之一: GetAssembly(Type t) GetCallingAssembly() ——返回調用當前方法的方法所在的程式集 Ge ...
  • 欲練神功,引刀自宮。為了避免記憶體管理的煩惱,Java咔嚓一下,把指針砍掉了。當年.Net也追隨潮流,咔嚓了一下,化名小桂子,登堂入室進了皇宮。康熙往下麵一抓:咦?還在?——原來是假太監韋小寶。 打開unsafe選項,C 指針就biu的一下子蹦出來了。指針很強大,沒必要拋棄這一強大的工具。誠然,在大多 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...