#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin>>T;
do{
vector<int> ans;
int N=0;
cin>>N;
vector<int> attackArray(N), defArray(N);
for (int i =0; i<N; i++) {
cin>>attackArray[i];
}
for (int i =0; i<N; i++) {
cin>>defArray[i];
}
for (int i =0; i<N; i++) {
int nexti, previ;
if (i == 0)
{
nexti = 1;
previ = N - 1;
}
else if (i == N - 1)
{
nexti = 0;
previ = N - 2;
}
else
{
nexti = i + 1;
previ = i - 1;
}
if (defArray[i] > attackArray[nexti] &&
defArray[i] > attackArray[previ] &&
defArray[i] > attackArray[nexti] + attackArray[previ]){
ans.push_back(defArray[i]);
}
else {ans.push_back(-1); break;}
}
sort(ans.begin(), ans.end(), greater<int>());
cout<<ans[0]<<endl;
T--;
}while (T !=0);
return 0;
}
Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N.
The second line contains N space-separated integers a1,a2,…,aN.
The third line contains N space-separated integers d1,d2,…,dN.
Output
For each test case, print a single line containing one integer ― the best defense value of the shield the king gets, or −1 if Chef can be thrown in the snake pit.
Example
2
4
1 1 4 1
3 4 2 1
7
5 4 5 4 5 4 5
3 2 4 7 2 5 9
Example Output
3
-1
The problem is that your sequence of if statements should be one if ... else if ... else statement.
if (i == 0)
...
else if (i==N-1)
...
else
...
Just because a previous if condiition has evaluated to true doesn't stop code after the if statement from executing unless you use an else. So the condition in your third if statement is executing even when i == 0 or i == N - 1, resulting in an out of bounds array access and a crash.
Also
int attackArray[N],defArray[N];
is not legal C++ because all array sizes must be compile time constants. You are clearly using a compiler which doesn't care, but you should, so use this instead
vector<int> attackArray(N), defArray(N);
Finally there's a lot of duplicated code in your different conditions. You could simplify a lot by adding a couple of extra variables for the next and previous values of i. E.g.
int nexti, previ;
if (i == 0)
{
nexti = 1;
previ = N - 1;
}
else if (i == N - 1)
{
nexti = 0;
previ = N - 2;
}
else
{
nexti = i + 1;
previ = i - 1;
}
if (defArray[i] > attackArray[nexti] &&
defArray[i] > attackArray[previ] &&
defArray[i] > attackArray[nexti] + attackArray[previ])
...
problem statement:
Johnny has some difficulty memorizing the small prime numbers. So, his computer science teacher has asked him to play with the following puzzle game frequently.
The puzzle is a 3x3 board consisting of numbers from 1 to 9. The objective of the puzzle is to swap the tiles until the following final state is reached:
1 2 3
4 5 6
7 8 9
At each step, Johnny may swap two adjacent tiles if their sum is a prime number. Two tiles are considered adjacent if they have a common edge.
Help Johnny to find the shortest number of steps needed to reach the goal state.
My solution so far
#include<bits/stdc++.h>
using namespace std;
bool prime[20];
int matrix[3][3];
int solved[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
void display()
{
for(int row = 0; row<3;row++)
{
for(int col = 0;col<3;col++)
{
cout<<matrix[row][col]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
}
bool check(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(matrix[i][j]!=solved[i][j])
return false;
}
}
return true;
}
int min(int a,int b)
{
return (a<b)?a:b;
}
void generate(){
memset(prime,true,sizeof(prime));
for(int i=2;i*i<20;i++){
if(prime[i]==true)
{
for(int j=2*i;j<20;j+=i)
prime[j]=false;
}
}
}
int getMoves(int row, int col){
if(row < 0 ||col< 0 || row>=3||col>=3){
return 0;
}
if(check()){
return 0;
}
int moves = 0;
for(int i = row-1 ; i<= row+1 ;i++)
{
for(int j = col -1 ; j<=col+1;j++)
{
if((i!=row-1&&j!=col-1)||(i!=row+1&&j!=col+1)||(i!=row+1&&j!=col-1)||(i!=row-1&&j!=col+1)){
if(prime[matrix[row][col]+matrix[i][j]]==true)
{
moves+=getMoves(i,j);
int temp;
temp = matrix[i][j];
matrix[i][j] = matrix[row][col];
matrix[row][col] = temp;
display();
}
}
}
}
return moves;
}
int Moves(){
int minMoves = INF;
for(int row = 0;row<3;row++)
{
for(int col = 0;col<3;col++)
{
int moves = getMoves(row,col);
minMoves = min(moves,minMoves);
}
}
return minMoves;
}
int main(){
generate();
int t;
cin>>t;
while(t--)
{
for(int row = 0; row<3;row++)
{
for(int col = 0;col<3;col++)
{
cin>>matrix[row][col];
}
}
}
cout<<Moves();
}
sample testcase
Input:
2
7 3 2
4 1 5
6 8 9
9 8 5
2 4 1
3 7 6
Output:
6
-1
the program keeps crashing I guess because of memory overflow issue.
if (row < 0 || col< 0 || row >= 3 || row <= 3) {
return 0;
}
The code after this part is 'not accessible' because this condition is always true (... row >= 3 || row <= 3). You probably meant to write: (... row >= 3 || col >= 3)
I'm afraid your code is completely wrong and I don't think it could by fixed without complete rewrite. For example in function getMoves() your variables i and j can acquire value -1 so you will face access violation error. Secondly you have a recursion there, but you don't change data before you invoke recursion. Let's assume you want to swap 7 and 4. In the next step (because you didn't change input) you can swap 4 and 1. But it's not a correct move, because in that time, 4 should't be there. Thirdly your function getMoves() can end in an endless loop.
In conclusion, these kinds of problem are solved quite differently. You can for exmaple use backtracking algorithm or you can use A* algorithm. You will have to evaluate your current state. Let assume the following state:
7 3 2
4 5 6
1 8 9
You can measure number of moves that the number has to do to go to its correct position. So in this case 1 has to do 2 moves, 7 has to do 2 moves, 2 has to do 1 move as well as the number 3. The value of this state is 2 + 2 + 1 + 1 = 6. It's called an heuristic function. Now you can take this function and put it in an A* algorithm, and you should see the correct result.
I've got a double[9] and want to check if it contains the values (1,0,0,0,1,0,0,0,1). Is there a cleaner way than this?
if (ornt1[0] == 1 && ornt1[1] == 0 && ornt1[2] == 0
&& ornt1[3] == 0 && ornt1[4] == 1 && ornt1[5] == 0
&& ornt1[6] == 0 && ornt1[7] == 0 && ornt1[8] == 1 )
I'm using C++.
It is not a good idea to compare double values strictly. I would recommend you create a constant array to compare against and then use a cycle and also use a tolerance(e.g. 1e-9):
bool doublesEqual(double a, double b) {
return fabs(a - b) < 1e-9;
}
const double expected[9] = {1,0,0,0,1, 0, 0, 0, 1};
bool equal = true;
for (int i = 0; i< 9; ++i) {
if (!doublesEqual(expected[i], ornt1[i])) {
equal = false;
break;
}
}
if (equal) { // do smth
EDIT: as suggested by John Zwinck I have edited the code to be able to handle the case when the array we compare contains only NAN. I have edited his suggestion a bit to make the code more readable. Please refer to his comment below for clarification why this is needed.
I am making a recursive algorithm as part of an extra credit project for class to find the shortest path through a 2D int array maze of 0's and 1's. 0 represents a wall and 1 represents a hallway where you can pass through. I think I have everything perfect but it just won't compile. It says I am trying to convert something from int to array or whatever. Here is my code, please help.
#include <iostream>
using namespace std;
#define 20 SIZEX;
#define 5 SIZEY;
int value; //to compare paths to take
int starti = 1;
int startj = 0;
int newi;
int newj;
int counter = 0; //keeps track of how many steps taken
void pathfinder(int a[][SIZEX], int currenti, int currentj);
int arr[SIZEY][SIZEX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,
0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int main()
{
pathfinder(arr, starti, startj);
system("PAUSE");
return 0;
}
void pathfinder(int a[][SIZEX], int currenti, int currentj)
{
//as soon as it walks somewhere, the value of that spot increments
int temp;
temp = a[currenti][currentj];
temp++;
a[currenti][currentj] = temp;
if (counter == 0) //keeps track of starting point
{
starti = currenti;
startj = currentj;
}
if (currenti-1 >= 0 && a[currenti-1][currentj] != 0) //checks up
{
value = a[currenti-1][currentj];
}
else if (currenti+1 < 5 && a[currenti+1][currentj] != 0) //checks down
{
value = a[currenti+1][currentj];
}
else if (currentj-1 >= 0 && a[currenti][currentj-1] != 0) //checks left
{
value = a[currenti][currentj-1];
}
else if (currentj+1 < 20 && a[currenti][currentj+1] != 0) //checks right
{
value = a[currenti][currentj+1];
}
//value has a value, now check it against all possible values for the least travelled path
if ((currenti-1 >= 0 && a[currenti-1][currentj] !=0) && value > a[currenti-1][currentj])
{
value = a[currenti-1][currentj];
}
if ((currenti+1 < 5 && a[currenti+1][currentj] !=0) && value > a[currenti+1][currentj])
{
value = a[currenti+1][currentj];
}
if ((currentj-1 >= 0 && a[currenti][currentj-1] != 0) && value > a[currenti][currentj-1])
{
value = a[currenti][currentj-1];
}
if ((currentj+1 < 20 && a[currenti][currentj+1] != 0) && value > a[currenti][currentj+1])
{
value = a[currenti][currentj+1];
}
//value now holds the smallest possible value among the four possible paths
if ((currenti-1 >= 0 && a[currenti-1][currentj] !=0) && value == a[currenti-1][currentj]) //move up
{
newi = currenti-1;
newj = currentj;
counter++;
}
else if ((currenti+1 < 5 && a[currenti+1][currentj] !=0) && value == a[currenti+1][currentj]) //move down
{
newi = currenti+1;
newj = currentj;
counter++;
}
else if ((currentj-1 >= 0 && a[currenti][currentj-1] != 0) && value == a[currenti][currentj-1]) //move left
{
newi = currenti;
newj = currentj-1;
counter++;
}
else if ((currentj+1 < 20 && a[currenti][currentj+1] != 0) && value == a[currenti][currentj+1]) //move right
{
newi = currenti;
newj = currentj+1;
counter++;
}
//upon reaching the exit, it will print out a new 2d maze, and the path with the smallest value of non-zero integers is the shortest path
if ((currenti == 0 || currentj == 0 || currenti == 4 || currentj == 19) && (currenti != starti || currentj !=startj))
{
for (int i = 0; i < 5; i++)
{
for (int j = 0;j < 20;j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return;
}
pathfinder(arr, newi, newj);
}
1>------ Build started: Project: Project5, Configuration: Debug Win32
------ 1> Source.cpp 1>c:\users\justin\documents\visual studio 2012\projects\project5\project5\source.cpp(22): error C2664:
'pathfinder' : cannot convert parameter 1 from 'int' to 'int [][20]'
1> Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast
1>c:\users\justin\documents\visual studio
2012\projects\project5\project5\source.cpp(114): error C2664:
'pathfinder' : cannot convert parameter 1 from 'int' to 'int [][20]'
1> Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
These are incorrect:
#define 20 SIZEX;
#define 5 SIZEY;
If you really want macros, the correct way to define them is:
#define SIZEX 20
#define SIZEY 5
But, as this is C++, you should make use of const int here:
const int SIZEX = 20;
const int SIZEY = 5;
That should fix your compile problem.
On a separate note: If you're going to pass arr as your initial argument to pathfinder() to start the recursion in main, you might change your recursive call to pass the argument pathfinder() was passed. ie:
pathfinder(a, newi, newj);
Otherwise, even if main were to pass a different array to pathfinder, it would still do most of its work on arr, rendering the parameter useless.
Recently I've been working on partition problem. I've done a research and I found that it can be solved using an algorithm on wiki page. Here's the pseudo algorithm:
INPUT: A list of integers S
OUTPUT: True if S can be partitioned into two subsets that have equal sum
1 function find_partition( S ):
2 N ← sum(S)
3 P ← empty boolean table of size (\lfloor N/2 \rfloor + 1) by (n + 1)
4 initialize top row (P(0,x)) of P to True
5 initialize leftmost column (P(x, 0)) of P, except for P(0, 0) to False
6 for i from 1 to \lfloor N/2 \rfloor
7 for j from 1 to n
8 P(i, j) ← P(i, j-1) or P(i-S[j-1], j-1)
9 return P(\lfloor N/2 \rfloor , n)
Using recursion you can calculate if certain sum from integers in array can be reached, if it can be reached it returns true. I start with sumOfTheIntegers/2 and I go back to 0, until I find a solution. When I found the biggest possible sum of the integers that is lower or equal to the average I calculate the difference between the the 2 groups of integers with (average-lowestSumLowerorEqualtoAverage)*2.
But then I confront with problem how can I include one dimensional array in the recursion?
Here's the code, it should probably work, but I haven't tested it yet, because of the problem. So maybe the code contains small errors. But that's not the problem, I'll fix it later.
#include <iostream>
#include <cmath>
using namespace std;
bool matrix (int a, int b)
{
if(b == -1) return true;
else if (a == -1) return false;
else if(matrix(a-1, b) == true) return true;
else if(matrix(a-1,b-numbers[a-1]) == true) return true;
else return false;
}
int main()
{
int number, sum = 0;
cin >> number;
int numbers[number];
for(int i = 0; i<number; i++)
{
cin >> numbers[i];
sum += numbers[i];
}
double average = sum/2.0;
for(int i = floor(sum/2); i!= 0; i--)
{
if(matrix(number+1, i) == true)
{
cout << abs(average-i)*2;
break;
}
}
return 0;
}
The easiest (but certainly not the best) way is to introduce a global variable:
#include <vector>
std::vector<int> numbers;
/* ... */
int main(){
int number;
cin >> number;
numbers.resize(number);
/* ... */
}
Another possibility is to use an additional parameter:
bool matrix (int a, int b, const std::vector<int>& numbers)
{
if(b == -1) return true;
else if (a == -1) return false;
else if(matrix(a-1, b,numbers) == true) return true;
else if(matrix(a-1,b-numbers[a-1],numbers) == true) return true;
else return false;
}
Note that int numbers[number] is actually using a compiler-specific extension (VLA) and is not part of the C++ standard (see Does C++ support Variable Length Arrays? and Why aren't variable-length arrays part of the C++ standard?).
Pass it as an argument to the function
bool matrix (int a, int b, int num_arr[])
{
...
matrix(a-1,b,num_arr);
...
}