7/28/2015

10855 - Rotated square

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. char Big_array[1000][1000];
  5. char small_array[4][1000][1000];
  6.  
  7. int checkin(int N, int n, int slice){
  8.         int counter = 0;
  9.         bool flag;
  10.  
  11.         for (int i = 0; i < N-n+1; i++){
  12.                
  13.                 for (int j = 0; j < N-n+1; j++){
  14.                         flag = true;
  15.                         for (int x = 0; x < n; x++){
  16.                                 for (int y = 0; y < n; y++){
  17.                                         if (Big_array[+ j][+ i] != small_array[slice][x][y]){
  18.                                                 flag = false; break;
  19.                                         }
  20.                                 }
  21.                                 if (!flag){
  22.                                         break;
  23.                                 }
  24.                         }
  25.                         if (flag){//the small square is found in the big one
  26.                                 ++counter;
  27.                         }
  28.                 }
  29.         }
  30.         return counter;
  31. }
  32.  
  33. int main()
  34. {
  35.         int N_big, n_small;
  36.         for (;;){
  37.                
  38.                 cin >> N_big >> n_small;
  39.                 if (N_big == 0 && n_small == 0) break;
  40.  
  41.                 int arr[4] = { 0 };
  42.  
  43.                 for (int i = 0; i < N_big; i++){
  44.                         for (int j = 0; j < N_big; j++){
  45.                                 cin >> Big_array[i][j];
  46.                         }
  47.                 }
  48.                 for (int i = 0; i < n_small; i++){
  49.                         for (int j = 0; j < n_small; j++){
  50.                                 cin >> small_array[0][i][j];
  51.                         }
  52.                 }
  53.  
  54.                 for (int i = 0; i < n_small; i++){
  55.                         for (int j = 0; j < n_small; j++){
  56.                                 /****rotated 90 degrees
  57.                                    (0,0) in slice#1 <-- (1,0) in slice#0
  58.                                    (0,1) in slice#1 <-- (0,0) in slice#0
  59.                                    (1,0) in slice#1 <-- (1,1) in slice#0
  60.                                    (1,1) in slice#1 <-- (0,1) in slice#0
  61.                                 */
  62.                                 small_array[1][i][j] = small_array[0][n_small - 1 - j][i];
  63.  
  64.                                 /****rotated 180 degrees
  65.                                    (0,0) in slice#2 <-- (1,1) in slice#0
  66.                                    (0,1) in slice#2 <-- (1,0) in slice#0
  67.                                    (1,0) in slice#2 <-- (0,1) in slice#0
  68.                                    (1,1) in slice#2 <-- (0,0) in slice#0
  69.                                 */
  70.                                 small_array[2][i][j] = small_array[0][n_small - 1 - i][n_small - 1 - j];
  71.  
  72.  
  73.                                 /****rotated 270 degrees
                                       (0,0) in slice#3 <-- (0,1) in slice#0
  74.                                    (0,1) in slice#3 <-- (1,1) in slice#0
  75.                                    (1,0) in slice#3 <-- (0,0) in slice#0
  76.                                    (1,1) in slice#3 <-- (1,0) in slice#0
  77.                                 */
  78.                                 small_array[3][i][j] = small_array[0][j][n_small - 1 - i];
  79.                         }
  80.                 }
  81.  
  82.                 for (int i = 0; i < 4; i++){
  83.                         arr[i] = checkin(N_big, n_small, i);
  84.                 }
  85.                 cout <<arr[0]<<" "<<arr[1]<<" "<<arr[2]<<" "<<arr[3]<< endl;
  86.         }
  87.  
  88.         return 0;
  89. }

7/11/2015

11034 - Ferry Loading IV

Note:
1) The cars are loaded in the order of their arrival;
2) The ferry is initially on the left bank;
*3) If you get the error "deque iterator not dereferenceable" , make sure that you are not calling  pop()/front() on empty queue. 

Here's the code:

  1. #include<iostream>
  2. #include<queue>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7.         int laps , l, m, c, car, load;
  8.         string bank;
  9.         bool rightBank;
  10.        
  11.         cin >> c;
  12.        
  13.         for (int i = 0; i < c; i++){
  14.                 cin >> l >> m;
  15.                
  16.                 queue<int>Left, Right;
  17.                 load = 0;
  18.                 rightBank = 0;
  19.                 for (int j = 0; j < m; j++){
  20.                         cin >> car >> bank;
  21.                         if (bank == "left")
  22.                                 Left.push(car);
  23.                         else
  24.                                 Right.push(car);
  25.                 }
  26.                 laps = 0;  l *= 100;  
  27.                 while (!Left.empty() || !Right.empty()){
  28.                
  29.                        
  30.                         load = 0;
  31.                         if (rightBank){
  32.                                 while (!Right.empty() && load + Right.front() <= l)
  33.                                 {
  34.                                         load += Right.front(); Right.pop();
  35.                                 }
  36.                         }
  37.                         else{
  38.                                 while (!Left.empty() && load + Left.front() <= l)
  39.                                 {
  40.                                         load += Left.front(); Left.pop();
  41.                                 }
  42.                         }
  43.                         ++laps;
  44.                         rightBank = 1 - rightBank;
  45.                 }
  46.                
  47.                 cout << laps << endl;          
  48.         }
  49.         return 0;
  50. }

Sample input: 
9
1  4 
100 right
100 right
100 right
100 right
1  3
20 left
81 left
99 left
1  1
100 right
15 4
380 right
720 left
1340 right
1040 left

Sample output: 
6
5
2
4

8/11/2013

846 - Steps - UVA

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.         int x , y;
  6.         int testCases;
  7.         int min_steps = 0;
  8.         cin>>testCases;
  9.        
  10.         for(int i=0; i<testCases ; i++)
  11.         {
  12.                 cin>>x>>y;
  13.                 int difference = y - x;
  14.                 min_steps = 0;
  15.                 if(difference != 0)
  16.                 {
  17.                          int sumOfSteps = 0;
  18.                       int z = 2; //divided by 2, it represents the size if the next step
  19.                         while(difference > sumOfSteps)
  20.                         {
  21.                                 sumOfSteps += (/ 2); //next step
  22.                                 min_steps++;
  23.                                 z++;
  24.                         }
  25.                 }
  26.                
  27.                 cout<<min_steps<<endl;
  28.         }
  29.         return 0;
  30. }


    You can find more of my solutions on
     Here