方法:dfs 联通分量
做做水题歇歇脑。
有几个容易出错的地方。第一要考虑骰子上至少是1点,所以如果一个骰子上没有点数,也要记作1。。。还有就是一个骰子全是点。
code:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 #include <stack> 8 #include <bitset> 9 #include <cstdlib> 10 #include <cmath> 11 #include <set> 12 #include <list> 13 #include <deque> 14 #include <map> 15 #include <queue> 16 #include <fstream> 17 #include <cassert> 18 #include <unordered_map> 19 #include <unordered_set> 20 #include <cmath> 21 #include <sstream> 22 #include <time.h> 23 #include <complex> 24 #include <iomanip> 25 #define Max(a,b) ((a)>(b)?(a):(b)) 26 #define Min(a,b) ((a)<(b)?(a):(b)) 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a)) 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a)) 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a)) 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a)) 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a)) 32 #define FOREACH(a,b) for (auto &(a) : (b)) 33 #define rep(i,n) FOR(i,0,n) 34 #define repn(i,n) FORN(i,1,n) 35 #define drep(i,n) DFOR(i,n-1,0) 36 #define drepn(i,n) DFOR(i,n,1) 37 #define MAX(a,b) a = Max(a,b) 38 #define MIN(a,b) a = Min(a,b) 39 #define SQR(x) ((LL)(x) * (x)) 40 #define Reset(a,b) memset(a,b,sizeof(a)) 41 #define fi first 42 #define se second 43 #define mp make_pair 44 #define pb push_back 45 #define all(v) v.begin(),v.end() 46 #define ALLA(arr,sz) arr,arr+sz 47 #define SIZE(v) (int)v.size() 48 #define SORT(v) sort(all(v)) 49 #define REVERSE(v) reverse(ALL(v)) 50 #define SORTA(arr,sz) sort(ALLA(arr,sz)) 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz)) 52 #define PERMUTE next_permutation 53 #define TC(t) while(t--) 54 #define forever for(;;) 55 #define PINF 1000000000000 56 #define newline ' ' 57 58 #define test if(1)if(0)cerr 59 using namespace std; 60 using namespace std; 61 typedef vector<int> vi; 62 typedef vector<vi> vvi; 63 typedef pair<int,int> ii; 64 typedef pair<double,double> dd; 65 typedef pair<char,char> cc; 66 typedef vector<ii> vii; 67 typedef long long ll; 68 typedef unsigned long long ull; 69 typedef pair<ll, ll> l4; 70 const double pi = acos(-1.0); 71 72 73 const int maxn = 50; 74 string board[maxn]; 75 int n, m; 76 int cnt; 77 vector<int> ans; 78 int dx[] = {-1,0,1,0}; 79 int dy[] = {0,1,0,-1}; 80 void dfs2(int x, int y, char c='*') 81 { 82 board[x][y] = c; 83 for (int d = 0; d < 4; ++d) 84 { 85 int nx = x+dx[d], ny = y+dy[d]; 86 if (nx < 0 || ny < 0 || nx >= n || ny >= m || board[nx][ny] != 'X') continue; 87 dfs2(nx, ny, c); 88 } 89 } 90 void dfs(int x, int y) 91 { 92 board[x][y] = '.'; 93 for (int d = 0; d < 4; ++d) 94 { 95 int nx = x+dx[d], ny = y+dy[d]; 96 if (nx < 0 || ny < 0 || nx >= n || ny >= m || board[nx][ny] == '.') continue; 97 if (board[nx][ny] == 'X') 98 { 99 ++cnt; dfs2(nx, ny); 100 } 101 dfs(nx, ny); 102 } 103 104 } 105 int main() 106 { 107 ios::sync_with_stdio(false); 108 cin.tie(0); 109 int kase = 0; 110 while (cin >> m >> n && m) 111 { 112 for (int r = 0; r < n; ++r) cin >> board[r]; 113 ans.clear(); 114 for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) 115 if (board[i][j] == '*') 116 { 117 cnt = 0; dfs(i, j); ans.push_back(max(cnt,1)); 118 } 119 for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (board[i][j] != '.') 120 { 121 ans.pb(1); 122 dfs2(i, j, '.'); 123 } 124 cout << "Throw " << ++kase << newline; 125 sort(ans.begin(), ans.end()); 126 bool first = true; 127 for (int i = 0; i < ans.size(); ++i) 128 { 129 if (first) first = false; 130 else cout << " "; 131 cout << ans[i]; 132 } 133 134 cout << newline << newline; 135 } 136 }