Source code for submission s1402

Go to diff to previous submission

ants.c

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define false 0
  4. #define true 1
  5.  
  6. char inv(char c){
  7. return c=='L' ? 'R' : 'L';
  8. }
  9. int main( int agrc, char * argv[] ) {
  10. char array[100002],c, remL, remR;
  11. int i, wood=10, ants, coo, remaining, pL, pR, timeL, timeR;
  12. int waitL, waitR;
  13. int distL, distR, distLast;
  14. int max=-1, len;
  15. while( scanf("%d %d", &wood, &ants ) != EOF ) {
  16. waitL = false;
  17. waitR = false;
  18. pL = wood;
  19. pR = 0;
  20. max = -1;
  21. memset(array, ' ', wood+1);
  22. for( i = 0 ; i < ants ; i++ ) {
  23. scanf("%d %c", &coo, &c );
  24. array[coo] = c;
  25. if(coo > pR) pR = coo;
  26. if(coo < pL) pL = coo;
  27. }
  28. remaining = ants;
  29. remL = 'L';
  30. remR = 'R';
  31. timeL = 0;
  32. timeR = wood;
  33. len = 0;
  34. while(timeL < wood) {
  35. if(array[timeL] == 'R'){
  36. max = wood-len;
  37. break;
  38. }
  39. timeL++;
  40. len++;
  41. }
  42. len = 0;
  43. while(timeR >= 0) {
  44. if(array[timeR] == 'L'){
  45. if(wood-len>max) max = wood-len;
  46. break;
  47. }
  48. timeR--;
  49. len++;
  50. }
  51. distL = -1;
  52. distR = -1;
  53. while(remaining && pL <= pR) {
  54. if(waitL == false){
  55. while(array[pL] == ' ') pL++;
  56. if(array[pL] == remL){
  57. remaining--;
  58. array[pL] = ' ';
  59. distL = pL;
  60. distLast = distL;
  61. pL++;
  62. } else if(array[pL] == inv(remL)) {
  63. remL = inv(remL);
  64. if(waitR == false)
  65. waitL = true;
  66. waitR = false;
  67. }
  68. }
  69. if(waitR == false && remaining){
  70. while(array[pR] == ' ') pR--;
  71. if(array[pR] == remR){
  72. remaining--;
  73. array[pR] = ' ';
  74. distR = pR;
  75. distLast = distR;
  76. pR--;
  77. } else if(array[pR] == inv(remR)){
  78. remR = inv(remR);
  79. if(waitL == false)
  80. waitR = true;
  81. waitL = false;
  82. }
  83. }
  84. /*for( i = 0 ; i < wood ; i++ )
  85. printf("%c", array[i]);
  86. printf(", rem: %c %c, p: %2d %2d, wait: %d %d, remaining: %d\n", remL, remR, pL, pR, waitL, waitR, remaining);*/
  87. }
  88. while(array[pL] == ' ') pL++;
  89. while(array[pR] == ' ') pR--;
  90.  
  91. /*printf("%d and %d\n", distL, distR);
  92. distL = array[pL] == ' ' ? wood+123 : pL;
  93. distR = array[pL] == ' ' ? wood+123 : wood-pR;*/
  94. printf("The last ant will fall down in %d seconds - ", max);
  95. if(distR > -1 && distL > -1 && (distL == wood-distR)){
  96. printf("started at %d and %d.\n", distL, distR);
  97. } else if(distR > -1 && (wood-distR > distL)){
  98. printf("started at %d.\n", distR);
  99. } else {
  100. printf("started at %d.\n", distL);
  101. }
  102. }
  103.  
  104. return 0;
  105. }
  106.  

Diff to submission s1302

ants.c