山東省第一屆ACM大學生程式設計競賽-Balloons(搜尋)
問題及程式碼:
Balloons
Time Limit: 1000MS Memory limit: 65536K
題目描述
Both Saya and Kudo like balloons. One day, they heard that in the central park, there will be thousands of people fly balloons to pattern a big image.
They were very interested about this event, and also curious about the image.
Since there are too many balloons, it is very hard for them to compute anything they need. Can you help them?
You can assume that the image is an N*N matrix, while each element can be either balloons or blank.
Suppose element A and element
B are both balloons. They are connected if:
i) They are adjacent;
ii) There is a list of element C1,C2,
… ,Cn, whileA
andC1 are connected,C1
andC2 are connected …Cn
and B are connected.
And a connected block means that every pair of elements in the block is connected, while any element in the block is not connected with any element out of the block.
To Saya, element A(xa,ya)and B(xb,yb) is adjacent if |xa-xb| + |ya-yb| ≤ 1
But to Kudo, element A(xa,ya) and element B (xb,yb) is adjacent if |xa-xb|≤1
and |ya-yb|≤1
They want to know that there’s how many connected blocks with there own definition of adjacent?
輸入
The first line of input in each test case contains one integer N (0<N≤100), which represents the size of the matrix.
Each of the next N lines contains a string whose length is N, represents the elements of the matrix. The string only consists of 0 and 1, while 0 represents a block and 1represents balloons.
The last case is followed by a line containing one zero.
輸出
示例輸入
5 11001 00100 11111 11010 10010 0
示例輸出
Case 1: 3 2
提示
來源
題意:
輸入一個n行n列的矩陣,其中:0 represents a block and 1represents balloons
根據以下條件
To Saya, elementA(xa,ya)and
B(xb,yb) is adjacent if |xa-xb| + |ya-yb| ≤ 1 表示是上下左右四個方向;
But to Kudo, element A(xa,ya) and element B (xb,yb) is adjacent if |xa-xb|≤1
and |ya-yb|≤1 表示是上下左右對角八個方向;
判斷連線在一起的“1”塊有幾組。
/*
* Copyright (c) 2016, 煙臺大學計算機與控制工程學院
* All rights reserved.
* 檔名稱:dfs.cpp
* 作 者:單昕昕
* 完成日期:2016年4月1日
* 版 本 號:v1.0
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<malloc.h>
using namespace std;
int book[101][101];
int Map[101][101];
int n,tx,ty;
int move1_x[4]= {0,1,0,-1};
int move1_y[4]= {-1,0,1,0};
int move2_x[8]= {-1,0,1,1,1,0,-1,-1};
int move2_y[8]= {-1,-1,-1,0,1,1,1,0};
void dfs1(int x,int y)
{
int i;
if (Map[x][y]==1&&book[x][y]==false)
{
book[x][y]=true;
for(i=0; i<4; i++)
{
tx=x+move1_x[i];//計算座標
ty=y+move1_y[i];
if (tx>=0&&ty>=0&&tx<n&&ty<n&&Map[tx][ty]==1&&book[tx][ty]==false)
{
dfs1(tx,ty);
}
}
return ;
}
}
void dfs2(int x,int y)
{
int i;
if (Map[x][y]==1&&book[x][y]==false)
{
book[x][y]=true;
for(i=0; i<8; i++)
{
tx=x+move2_x[i];//計算座標
ty=y+move2_y[i];
if (tx>=0&&ty>=0&&tx<n&&ty<n&&Map[tx][ty]==1&&book[tx][ty]==false)
{
dfs2(tx,ty);
}
}
return ;
}
}
int main()
{
int Case=0;
while(cin>>n&&n!=0)
{
memset(book,false,sizeof(book));
int i,j,sum1=0,sum2=0;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
cin >> Map[i][j];
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(Map[i][j]==1&&book[i][j]==false)
{
dfs1(i,j);
++sum1;
}
}
memset(book,false,sizeof(book));
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(Map[i][j]==1&&book[i][j]==false)
{
dfs2(i,j);
++sum2;
}
}
cout<<"Case "<<++Case<<": "<<endl;
cout<<sum1<<" "<<sum2<<endl;
}
return 0;
}
/*
5
1 1 0 0 1
0 0 1 0 0
1 1 1 1 1
1 1 0 1 0
1 0 0 1 0
*/
執行結果:
相關文章
- 第十屆山東省大學生程式設計競賽題解(A、F、M、C)程式設計
- 第15屆浙江省大學生程式設計競賽D題程式設計
- 第二十屆西南科技大學ACM程式設計競賽(同步賽)ACM程式設計
- 2020 年第一屆遼寧省大學生程式設計競賽 D.開心消消樂(點分治)程式設計
- 2019山東ACM省賽補題題解ACM
- 無錫學院2024年ACM大學生程式設計競賽校選賽 題解ACM程式設計
- [題解][2021-2022年度國際大學生程式設計競賽第10屆陝西省程式設計競賽] Type The Strings程式設計
- 紹興市大學生程式設計競賽程式設計
- 第 10 屆 CCPC 中國大學生程式設計競賽濟南站 遊記程式設計
- 24山東省賽wp
- QZEZ第一屆“飯吉圓”杯程式設計競賽程式設計
- [補題] 第 45 屆國際大學生程式設計競賽(ICPC)亞洲區域賽(上海)程式設計
- 華中農業大學第十三屆程式設計競賽程式設計
- 第十屆中國大學生程式設計競賽 重慶站(CCPC 2024 Chongqing Site)程式設計
- 第43屆ACM-ICPC國際大學生程式設計競賽 亞洲區域賽南京站現場賽名額分配相關說明ACM程式設計
- 2024 CCPC第五屆遼寧省程式設計競賽 集訓2程式設計
- 九州信泰杯 第十一屆山東省網路安全技能大賽
- 華中農業大學第十三屆程式設計競賽 題解程式設計
- 【比賽覆盤】2024第七屆“傳智杯”全國大學生計算機大賽程式設計挑戰賽(初賽第一場)計算機程式設計
- 北京資訊科技大學第十一屆程式設計競賽(重現賽)I程式設計
- 湖南大學2020屆ACM新生賽 部分題解ACM
- 大學生電子設計競賽電源資料
- 第十五屆浙江大學寧波理工學院程式設計大賽(同步賽)程式設計
- 2020年“感恩杯”台州學院第十三屆大學生程式設計競賽D、H、I題解(後續補充)程式設計
- 10.5組隊訓練賽-2024CCPC山東省賽
- 第十七屆中國計量大學程式設計競賽 I- Isolated Pointset程式設計
- 2019年第二屆全國大學生大資料技能競賽通知大資料
- 2024CCPC山東省賽補題記錄
- M-災難預警-浙江農林大學第十九屆程式設計競賽暨天梯賽選拔賽程式設計
- 第十四屆全國大學生資訊保安競賽——創新實踐能力賽(東北賽區)比賽圓滿落幕
- 第二屆“重科杯”重慶科技大學程式設計競賽(同步賽)ptlks的題解(2024.5.18)程式設計
- “九韶杯”河科院程式設計協會第一屆程式設計競賽題目分析以及總結程式設計
- “位元組跳動杯”2018中國大學生程式設計競賽-女生專場程式設計
- 第九屆中國大學生程式設計競賽 深圳站(CCPC 2023 Shenzhen Site)/ The 2nd Universal Cup. Stage 25: Shenzhen程式設計
- 中國大學生數學競賽(非數學專業類)競賽大綱
- 程式設計天才“樓教主”—— 專訪兩屆“黑客杯”世界程式設計大賽季軍、清華大學博士生樓天城...程式設計黑客
- 【牛客訓練記錄】浙江機電職業技術大學第九屆程式設計競賽程式設計
- 競真我 贏未來:首屆大學生數字體育競技大賽釋出會召開
- 2023 國際大學生程式設計競賽亞洲區域賽(濟南站)(SMU Autumn 2024 Team Round 2)程式設計