WhaleSpunk

Treino W

Apr 4th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int tabuleiro[9][9]={{0}};
  5. int movimentoX[4]={-1,0,1,0};
  6. int movimentoY[4]={0,-1,0,1};
  7. int num=10000;
  8.  
  9.  
  10. void minMovimentos(int n, int posX, int posY, int numMovimentos,int totalPretas,int numAtual){
  11. int i, flag,aux;
  12.  
  13. if(numMovimentos>=num){
  14. return;
  15. }
  16.  
  17. if(numAtual==totalPretas){
  18.  
  19. if(numMovimentos<num){
  20. num=numMovimentos;
  21.  
  22. }
  23.  
  24. return;
  25. }else{
  26.  
  27. for(i=0;i<4;i++){
  28.  
  29. if(tabuleiro[posX+movimentoX[i]][posY+movimentoY[i]]!=2 && posX+movimentoX[i]<n && posX+movimentoX[i]>=0 && posY+movimentoY[i]<n && posY+movimentoY[i]>=0){
  30.  
  31. if(tabuleiro[posX+movimentoX[i]][posY+movimentoY[i]]==1){
  32. numAtual++;
  33. flag=1;
  34. }
  35.  
  36. aux=tabuleiro[posX+movimentoX[i]][posY+movimentoY[i]];
  37.  
  38. tabuleiro[posX+movimentoX[i]][posY+movimentoY[i]]=2;
  39.  
  40. minMovimentos(n,posX+movimentoX[i],posY+movimentoY[i],numMovimentos+1,totalPretas,numAtual);
  41.  
  42. tabuleiro[posX+movimentoX[i]][posY+movimentoY[i]]=aux;
  43.  
  44. if(flag==1){
  45. numAtual--;
  46. flag=0;
  47. }
  48. }
  49.  
  50. }
  51.  
  52. }
  53.  
  54. }
  55.  
  56. int main()
  57. {
  58. int i,j,k,n,casos_teste, inicioX=0, inicioY=0, totalPretas=0;
  59. scanf("%d",&casos_teste);
  60. for(i=0;i<casos_teste;i++){
  61. scanf("%d",&n);
  62. for(j=0;j<n;j++){
  63. for(k=0;k<n;k++){
  64. scanf("%d",&tabuleiro[j][k]);
  65. if(tabuleiro[j][k]==2){
  66. inicioX=j;
  67. inicioY=k;
  68. }else if(tabuleiro[j][k]==1){
  69. totalPretas++;
  70. }
  71. }
  72. }
  73.  
  74. if(totalPretas==0){
  75. printf("0\n");
  76. }else if(totalPretas==((n*n)-1)){
  77. printf("%d\n",((n*n)-1));
  78. }
  79. else{
  80.  
  81. minMovimentos(n,inicioX,inicioY,0,totalPretas,0);
  82. printf("%d\n",num);
  83.  
  84. }
  85. num=10000;
  86. totalPretas=0;
  87.  
  88. }
  89. return 0;
  90. }
Add Comment
Please, Sign In to add comment