fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3. using namespace std;
  4.  
  5.  
  6. struct Celula{
  7. int el;
  8. Celula *prox;
  9. };
  10.  
  11. struct Celula *topo;
  12. int nelem;
  13.  
  14.  
  15. void construidor(){
  16.  
  17. topo=NULL;
  18. nelem=0;
  19.  
  20. }
  21. void destruidor(){
  22. Celula *aux;
  23. while(topo){
  24. aux=topo;
  25. topo=topo->prox;
  26. delete aux;
  27. }
  28. }
  29.  
  30. void empilha (const int &el){
  31.  
  32. Celula *nova = new Celula;
  33. if(nova==NULL){
  34. cout<<"sem memória"<<endl;
  35. }
  36.  
  37. nova->prox=topo;
  38. nova->el=el;
  39. topo=nova;
  40. nelem++;
  41. }
  42.  
  43. int desempilha()throw(logic_error){
  44.  
  45. if(nelem==0) {
  46. throw logic_error("Pilha Vazia\n");
  47. }
  48.  
  49.  
  50.  
  51. int ret = topo->el;
  52. Celula *aux =topo;
  53. topo=topo->prox;
  54. delete aux;
  55. nelem --;
  56. return ret;
  57.  
  58.  
  59. }
  60.  
  61. bool vazia(){
  62. return (nelem==0);
  63. }
  64.  
  65. int tamanho(){
  66. return (nelem);
  67.  
  68. }
  69.  
  70. int main(){
  71.  
  72. char entrada;
  73.  
  74.  
  75. construidor();
  76.  
  77.  
  78. int L,C;
  79.  
  80.  
  81. cin>>L;
  82. cin>>C;
  83.  
  84. char matriz[L][C];
  85. char caminho[L][C];
  86.  
  87. for(int i=0;i<L;i++){
  88. for(int j=0;j<C;j++){
  89. cin>>entrada;
  90. matriz[i][j]=entrada;
  91. caminho[i][j]=entrada;
  92. }
  93. }
  94.  
  95. int x,y;
  96. while((x!=-1)&&(y!=-1)){
  97. cin>>x;
  98. cin>>y;
  99. if((x!=-1)&&(y!=-1)){
  100. bool preenchido=true;
  101.  
  102. empilha(x);
  103. empilha(y);
  104. int soma=0;
  105.  
  106. while(!vazia()){
  107. y=desempilha();
  108. //cout<<y<<endl;
  109. x=desempilha();
  110. //cout<<x<<endl;
  111.  
  112. if((y>=0)&&(y<C)){
  113. if((x>=0)&&(x<L)){
  114. if(caminho[x][y]!='x'){
  115. caminho[x][y]='x';
  116. empilha(x);
  117. empilha(y+1);
  118. empilha(x-1);
  119. empilha(y);
  120. empilha(x);
  121. empilha(y-1);
  122. empilha(x+1);
  123. empilha(y);
  124. //cout<<matriz[x][y]<<endl;
  125. soma=soma+1;
  126. preenchido=false;
  127.  
  128.  
  129.  
  130.  
  131. }
  132. }
  133. }
  134.  
  135. }
  136. if(preenchido!=true){
  137. cout<<soma<<endl;
  138. }
  139. }
  140. for(int i=0;i<L;i++){
  141. for(int j=0;j<C;j++){
  142. caminho[i][j]=matriz[i][j];
  143.  
  144. }
  145. }
  146. destruidor();
  147. }
  148. return 0;
  149. }
  150.  
  151.  
  152.  
Success #stdin #stdout 0s 3280KB
stdin
4 5
000x0
000x0
000x0
000x0
0 3
0 0
0 1
0 2
0 3
-1 -1
stdout
12
12
12