zoj 1654 Place the Robots 最大二分匹配

來源:互聯網
上載者:User

        轉換成最大匹配。

        每個橫向的'o’和'#’塊(必須包含'o’)作為X集合一個頂點,每個縱向的'o’和'#’塊(必須包含'o’)作為Y集合一個頂點

        當橫向塊和縱向塊的交點的'o’時,二分圖該兩個頂點有邊相串連。

        這題因為下標不小心打錯了,調了很久才ac,納悶。

 

#include <iostream>#include <cstring>using namespace std;const int N = 55;const int MAX = N*N;struct hori_block{int row;int col_left, col_right;};struct ver_block{int col;int row_up, row_dow;};hori_block X[MAX];ver_block Y[MAX];char map[N][N];bool maze[MAX][MAX];int match[MAX];bool isvisit[MAX];int row, col;int cnt_x, cnt_y;void build_graph(){cnt_x = 0;cnt_y = 0;for (int i = 0; i < row; i++)for (int j = 0; j < col; j++){if (map[i][j] == 'o'){X[cnt_x].row = i;for (int k = j; k >= 0; k--)if (map[i][k] != '#')X[cnt_x].col_left = k;elsebreak;while (j < col && map[i][j] != '#'){X[cnt_x].col_right = j;j++;}cnt_x++;}}for (int j = 0; j < col; j++)for (int i = 0; i < row; i++){if (map[i][j] == 'o'){Y[cnt_y].col = j;for (int k = i; k >= 0; k--)if (map[k][j] != '#') Y[cnt_y].row_up = k;elsebreak;while (i < row && map[i][j] != '#'){Y[cnt_y].row_dow = i;i++;}cnt_y++;}}memset(maze, false, sizeof(maze));for (int i = 0; i < cnt_x; i++)for (int j = 0; j < cnt_y; j++)if (X[i].row >= Y[j].row_up && X[i].row <= Y[j].row_dow&& Y[j].col >= X[i].col_left && Y[j].col <= X[i].col_right&& map[ X[i].row ][ Y[j].col ] == 'o')maze[i][j] = true;}bool find(int u){for (int i = 0; i < cnt_y; i++){if (maze[u][i] && !isvisit[i]){isvisit[i] = true;if (match[i] == -1 || find(match[i])){match[i] = u;return true;}}}return false;}int max_match(){int ans = 0;memset(match, -1, sizeof(match));for (int i = 0; i < cnt_x; i++){memset(isvisit, false, sizeof(isvisit));if (find(i))ans++;}return ans;}int main(){int cases;cin >> cases;for (int k = 1; k <= cases; k++){cin >> row >> col;for (int i = 0; i < row; i++)for (int j = 0; j < col; j++)cin >> map[i][j];build_graph();cout << "Case :" << k << endl;cout << max_match() << endl;}return 0;}

聯繫我們

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