X419-01: Take-Home Assignment 1 Assume you are given a function bool hit(int x,
ID: 3912767 • Letter: X
Question
X419-01: Take-Home Assignment 1 Assume you are given a function bool hit(int x, int y) that returns true if there was a hit to a ship and false if not. You are to write a function that returns the coordinates of a single ship on a grid of given size, the function takes at least one argument int gridSize and must return the coordinates of the 3 unit ship present in the board in the shortest time possible. Estimate the Time and memory complexity of your proposed solution. You are to select the data structure to return what you need as output and the prototype of the function.Explanation / Answer
here is your function : ----------->>>>>>>>>
#include<iostream>
using namespace std;
typedef struct coord_{
int x;
int y;
}Coordinate;
bool hit(int x,int y){
//just a prototype
return false;
}
Coordinate* coord_of_singleship(int gridSize){
Coordinate *res = new Coordinate[3];
for(int i = 0;i<gridSize;i++){
for(int j = 0;j<gridSize;j++){
//if it find one place where it hit
//below lines inside the if will check all Posibility of the ship orientation
//and find all coordinate and return all three cordinate
if(hit(i,j)){
res[0].x = i;
res[0].y = j;
//horizontal
if(j != 0 && j != gridSize-1)
if(hit(i,j+1) && hit(i,j-1)){
res[1].x = i;
res[1].y = j+1;
res[2].x = i;
res[2].y = j-1;
return res;
}
if(j < gridSize-2)
if(hit(i,j+1) && hit(i,j+2)){
res[1].x = i;
res[1].y = j+1;
res[2].x = i;
res[2].y = j+2;
return res;
}
if(j > 1)
if(hit(i,j-1) && hit(i,j-2)){
res[1].x = i;
res[1].y = j-1;
res[2].x = i;
res[2].y = j-2;
return res;
}
//vertically
if(i != 0 && i != gridSize-1)
if(hit(i+1,j) && hit(i-1,j)){
res[1].x = i+1;
res[1].y = j;
res[2].x = i-1;
res[2].y = j;
return res;
}
if(i < gridSize-2)
if(hit(i+1,j) && hit(i+2,j)){
res[1].x = i+1;
res[1].y = j;
res[2].x = i+2;
res[2].y = j;
return res;
}
if(i > 1)
if(hit(i-1,j) && hit(i-2,j)){
res[1].x = i-1;
res[1].y = j;
res[2].x = i-2;
res[2].y = j;
return res;
}
//diagonal
if(i != 0 && j != 0 && i != gridSize-1 && j != gridSize-1)
if(hit(i-1,j-1) && hit(i+1,j+1)){
res[1].x = i-1;
res[1].y = j-1;
res[2].x = i+1;
res[2].y = j+1;
return res;
}
if(i > 2 && j > 2)
if(hit(i-1,j-1) && hit(i-2,j-2)){
res[1].x = i-1;
res[1].y = j-1;
res[2].x = i-2;
res[2].y = j-2;
return res;
}
if(i < gridSize-2 && j < gridSize-2)
if(hit(i+1,j+1) && hit(i+2,j+2)){
res[1].x = i+1;
res[1].y = j+1;
res[2].x = i+2;
res[2].y = j+2;
return res;
}
if(i != 0 && j != 0 && i != gridSize-1 && j != gridSize-1)
if(hit(i-1,j+1) && hit(i+1,j-1)){
res[1].x = i-1;
res[1].y = j+1;
res[2].x = i+1;
res[2].y = j-1;
return res;
}
if(i < gridSize-2 && j > 2)
if(hit(i+1,j-1) && hit(i+2,j-2)){
res[1].x = i+1;
res[1].y = j-1;
res[2].x = i+2;
res[2].y = j-2;
return res;
}
if(i > 2 && j < gridSize-2)
if(hit(i-1,j+1) && hit(i-2,j+2)){
res[1].x = i-1;
res[1].y = j+1;
res[2].x = i-2;
res[2].y = j+2;
return res;
}
}
}
}
return NULL;
}
int main(){
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.