Advertisement
Guest User

UVA469 UKDW

a guest
Nov 14th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <string>
  5. #include <sstream>
  6. #include <queue>
  7.  
  8. using namespace std;
  9.  
  10. typedef pair<int, int> ii;
  11. typedef vector<int> vi;
  12. typedef vector<ii> vii;
  13.  
  14. // ini buat cek semua arah
  15. int dr[] = {-1, -1, 0, 1, 1, 1, 0, -1};
  16. int dc[] = {0, 1, 1, 1, 0, -1, -1, -1};
  17.  
  18.  
  19. int bfs(ii P, vector<string> data){
  20. queue<ii> q;
  21. q.push(P);
  22. int area = 0;
  23.  
  24. while(!q.empty()){
  25. ii p = q.front(); q.pop();
  26. area++;
  27. data[ p.first ][ p.second ] = 'L';
  28. for(int i=0;i<8;i++){
  29. int r = p.first + dr[i];
  30. int c = p.second + dc[i];
  31.  
  32. if(r >= 0 && r < data.size() && c >= 0 && c < data[0].length() ){
  33. if( data[ r ][ c ] == 'W' ){
  34. data[r][c] = 'L';
  35. q.push( ii(r, c) );
  36. }
  37. }
  38. }
  39. }
  40. return area;
  41. }
  42.  
  43.  
  44. int main(){
  45. int T;
  46. string input;
  47. cin >> T;
  48. cin.ignore();
  49. getline(cin, input);
  50.  
  51. bool first = true;
  52. while(T--){
  53. if(first){ first = false; }else{cout << endl;} // blank lines between output
  54.  
  55. vii L;
  56. vector<string> data;
  57. while(true){
  58. istringstream iss;
  59. string line;
  60. getline(cin, line);
  61. if(line.length() == 0 ){
  62. break;
  63. }
  64. if(isdigit(line[0])){
  65. iss.str(line);
  66. int A, B;
  67. iss >> A;
  68. iss >> B;
  69. L.push_back( ii(--A, --B)); // -- karena index yang dipakai dari 0
  70. }else{
  71. data.push_back(line);
  72. }
  73. }
  74. vii::iterator it;
  75. for(it = L.begin(); it != L.end(); it++){
  76. cout << bfs( (*it), data ) << endl;
  77. }
  78.  
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement