Heyo,
I' am writing a game for my uni project, and can't get a piece of my code to detect if they completed the game correctly. The game is https://brainbashers.com/abcview.asp (https://www.brainbashers.com/showabcview.asp?date=1206&which=3). I am checking if there are only 1 A, only 1B, only 1 C in a line/column. Checked it and it does not detect the letter written. Does Anyone have some ideas?
int a = 0, b = 0, c = 0;
for (int i = 3; i < 9; i++)
{
for (int j = 3; j < 9; j++)
{
if (gamefield[i][j] == 'A')
a++;
else if (gamefield[i][j] == 'B')
b++;
else if (gamefield[i][j] == 'C')
c++;
}
if (a == 1 || b == 1 || c == 1)
{
}
else {return false; }
a = 0; b = 0; c = 0;
}
a = 0; b = 0; c = 0;
for (int i = 3; i < 9; i++)
{
for (int j = 3; j < 9; j++)
{
if (gamefield[j][i] == 'A')
a++;
else if (gamefield[j][i] == 'B')
b++;
else if (gamefield[j][i] == 'C')
c++;
}
if (a == 1 || b == 1 || c == 1)
{
}
else {
return false;
}
a = 0; b = 0; c = 0;
}```
can you try to use the ASCII values of the letter instead of the letter itself,
for example to verify if a letter equals 'A' you would write :
if (gamefield[j][i] == 65)
Related
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int row = obstacleGrid.size();
int col = obstacleGrid[0].size();
if(obstacleGrid[0][0] == 1){
return 0;
}
if(row = 1 && col == 1){
return 1;
}
vector<vector<int>> dp(row, vector<int>(col, 0));
bool flag = false;
if(col > 1){
for(int i = 0; i < col; i++){
if(flag == true || obstacleGrid[0][i] == 1){
flag = true;
dp[0][i] = 0;
}else{
dp[0][i] = 1;
}
}
if(row == 1){
return dp[0][col - 1];
}
}
flag = false;
if(row > 1){
for(int j = 1; j < row; j++){
if(flag == true || obstacleGrid[j][0] == 1){
flag == true;
dp[j][0];
}else{
dp[j][0] = 1;
}
}
if(col == 1){
return dp[row - 1][0];
}
}
//fill the rest
for(int i = 1; i < row; i++){
for(int j = 1; j < col; j++){
if(obstacleGrid[i][j] == 1){
dp[i][j] = 0;
}else{
dp[i][j] = (dp[i-1][j] + dp[i][j-1]);
}
}
}
return dp[row - 1][col - 1];
}
Testcase
Run Code Result
Debugger
Runtime Error
Line 1034: Char 9: runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int>>' (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:9
For reference, this is question 58 on leetcode (Unique Paths II). I managed to narrow down the source of the error to this part of the code
if(col > 1){
for(int i = 0; i < col; i++){
if(flag == true || obstacleGrid[0][i] == 1){
flag = true;
dp[0][i] = 0;
}else{
dp[0][i] = 1;
}
}
if(row == 1){
return dp[0][col - 1];
}
}
The strange thing is it works correctly on the WSL debugger for VSCode, using parameter {{0,0,0}, {0,1,0}, {0,0,0}} it returns "2" as its supposed to. Also, it didn't have this issue until I put in conditions (col > 1) and (row > 1) to avoid buffer overflow for corner cases with only 1 column or row respectively.
Correct the typos, and it works fine.
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int row = obstacleGrid.size();
int col = obstacleGrid[0].size();
if(obstacleGrid[0][0] == 1){
return 0;
}
if(row == 1 && col == 1){ // First typo. row == 1 instead of row = 1
return 1;
}
vector<vector<int>> dp(row, vector<int>(col, 0));
bool flag = false;
if(col > 1){
for(int i = 0; i < col; i++){
if(flag == true || obstacleGrid[0][i] == 1){
flag = true;
dp[0][i] = 0;
}else{
dp[0][i] = 1;
}
}
if(row == 1){
return dp[0][col - 1];
}
}
flag = false;
if(row > 1){
for(int j = 1; j < row; j++){
if(flag == true || obstacleGrid[j][0] == 1){
flag = true; //second one. flag = true instead of flag == true
dp[j][0] = 0; //third. no value assigned.
}else{
dp[j][0] = 1;
}
}
if(col == 1){
return dp[row - 1][0];
}
}
//fill the rest
for(int i = 1; i < row; i++){
for(int j = 1; j < col; j++){
if(obstacleGrid[i][j] == 1){
dp[i][j] = 0;
}else{
dp[i][j] = (dp[i-1][j] + dp[i][j-1]);
}
}
}
return dp[row - 1][col - 1];
}
I'm trying to solve this question. My solution is almost identical to the fastest one. But for a test case, I have time limit exceeded while the other (the fastest one) does not. Could anybody explain for me why my code is so slow?. Here is my code:
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int M = grid.size();
if(M == 0) return 0;
int N = grid[0].size();
if(N == 0) return 0;
int flag = 2;
int count = 0;
for(int i = 0; i < M; ++i){
for(int j = 0; j < N; ++j){
if(grid[i][j] == '1'){
//breadth-first search from here
flag++;
count++;
queue<pair<int, int>> nodes;
grid[i][j] = flag;
nodes.push({i,j});
while(!nodes.empty()){
auto node = nodes.front();
nodes.pop();
if(node.first > 0 && grid[node.first-1][node.second] == '1'){
grid[node.first-1][node.second] = flag;
nodes.push(make_pair(node.first-1, node.second));
}
if(node.first < M-1 && grid[node.first+1][node.second] == '1'){
grid[node.first+1][node.second] = flag;
nodes.push(make_pair(node.first+1, node.second));
}
if(node.second > 0 && grid[node.first][node.second-1] == '1'){
grid[node.first][node.second-1] = flag;
nodes.push(make_pair(node.first, node.second-1));
}
if(node.second < N-1 && grid[node.first][node.second + 1] == '1'){
grid[node.first][node.second+1] = flag;
nodes.push(make_pair(node.first, node.second+1));
}
}
}
}
}
return count;
}
};
Here is the fastest solution. The author was very clever to use the array offsets, and I think that's the only difference between his code and mine. But I don't think it speed up the code.
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int m = grid.size(), n = m ? grid[0].size() : 0, islands = 0, offsets[] = {0, 1, 0, -1, 0};
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
islands++;
grid[i][j] = '0';
queue<pair<int, int>> todo;
todo.push({i, j});
while (!todo.empty()) {
pair<int, int> p = todo.front();
todo.pop();
for (int k = 0; k < 4; k++) {
int r = p.first + offsets[k], c = p.second + offsets[k + 1];
if (r >= 0 && r < m && c >= 0 && c < n && grid[r][c] == '1') {
grid[r][c] = '0';
todo.push({r, c});
}
}
}
}
}
}
return islands;
}
};
The problem here is that you are overwriting the islands on grid with the value of flag. When the value of flag gets equal to '1', then your code enters an infinite cycle because you are asking for cell with '1' for detecting islands.
With this extra line change on your code I got accepted on the problem.
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int M = grid.size();
if(M == 0) return 0;
int N = grid[0].size();
if(N == 0) return 0;
int flag = 2;
int count = 0;
for(int i = 0; i < M; ++i){
for(int j = 0; j < N; ++j){
if(grid[i][j] == '1'){
//breadth-first search from here
flag++;
if (flag == '1') flag++; /////THIS LINE HERE
count++;
queue<pair<int, int>> nodes;
grid[i][j] = flag;
nodes.push({i,j});
while(!nodes.empty()){
auto node = nodes.front();
nodes.pop();
if(node.first > 0 && grid[node.first-1][node.second] == '1'){
grid[node.first-1][node.second] = flag;
nodes.push(make_pair(node.first-1, node.second));
}
if(node.first < M-1 && grid[node.first+1][node.second] == '1'){
grid[node.first+1][node.second] = flag;
nodes.push(make_pair(node.first+1, node.second));
}
if(node.second > 0 && grid[node.first][node.second-1] == '1'){
grid[node.first][node.second-1] = flag;
nodes.push(make_pair(node.first, node.second-1));
}
if(node.second < N-1 && grid[node.first][node.second + 1] == '1'){
grid[node.first][node.second+1] = flag;
nodes.push(make_pair(node.first, node.second+1));
}
}
}
}
}
return count;
}
};
Note: This code was only for ilustrate the error, that doesn't mean that is an elegant solution for the bug.
I've been trying to figure out the problem behind this for a few days. I think it's counting the neighbours incorrectly because when I print the counts, the numbers are mostly 1s and 2s and my output board is completely blank. X ('X') means alive and ' ' means dead.
void NextGen(char lifeBoard[][MAX_ARRAY_SIZE], int numRowsInBoard, int numColsInBoard) {
char nexGenBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE];
// initialize nexGenBoard to blanks spaces
for(int i = 0; i < numRowsInBoard; i++) {
for(int j = 0; j < numColsInBoard; j++) {
nexGenBoard[i][j] = {' '};
}
}
// start from i = 1 and j = 1 to ignore the edge of the board
for(int i = 1; i < numRowsInBoard-1; i++) {
for(int j = 1; j < numColsInBoard-1; j++) {
int count = 0;
for(int y = -1; y < 2; y++) {
for(int x = -1; x < 2; x++) {
if(!(x == 0 || y == 0)) {
if(lifeBoard[i+y][j+x] == X) //X is a global constant of 'X'.
{
count++;
}
}
}
}
if(lifeBoard[i][j] == X) {
if(count == 2 || count == 3) {
nexGenBoard[i][j] = X;
}
}
else if(lifeBoard[i][j] == ' ') {
if(count == 3) {
nexGenBoard[i][j] = X;
}
}
}
}
for(int i = 0; i < numRowsInBoard; i++) {
for(int j = 0; j < numColsInBoard; j++) {
lifeBoard[i][j] = nexGenBoard[i][j];
}
}
}
Your check during counting (!(x == 0 || y == 0)) is wrong. This will not check the square if either x or y is zero. You want to not count if both x and y are zero.
if (!(x == 0 && y == 0))
or
if (x != 0 || y != 0)
what I want to do is :
Input a sentence from the user. Use full stop, space and comma as word separators. Each word should be stored in a 2D array whose columns vary in size and each row stores one word as a NULL terminated string.
For example, if the user inputs:
Hello how are you?
It should be stored as:
H E l l o NULL
h o w NULL
a r e NULL
y o u ? NULL
so whenever I try to run my code either this error appears
Exception thrown at 0x00832605 in Project109.exe: 0xC0000005: Access violation writing location 0xFDFDFE03.
or the program stops working.major problem is in
ptr[i][j] = str1[j];
`
char str1[20];
cin.get(str1, 20);
int len, sum = 0;
len = strlen(str1);
int i = 0;
while (str1[i] != '\0')
{
if (str1[i] == ' ' || str1[i] == '.' || str1[i] == ',' || str1[i] == '?' || str1[i] == ';')
{
sum = sum + 1;
}
i++;
}
char **ptr;
ptr = new char*[sum];
for (int i = 0; i < sum; i++)
{
ptr[i] = new char[20];
}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < 20; j++)
{
ptr[i][j] = '\0';
}}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < 20; j++)
{
if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
{
i++;
}
else
{
ptr[i][j] = str1[j];
}
}
}
for (int i = 0; i < sum; i++)
{
int j = 0;
while (ptr[i][j] != '\0')
{
cout << ptr[i][j];
j++;
}
}
for (int i = 0; i < sum; i++)
{
delete[] ptr[i];
}
delete[] ptr;
system("pause");
return 0;}
`
You are indexing out of range, and hitting memory with fence bytes containing 0xFD.
Consider the loop here
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < 20; j++)
{
if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
{
i++;
}
else
{
ptr[i][j] = str1[j];
}
}
}
If i is already at (or near) it's maximum value, in the inner loop you might increment it one or more times before reaching ptr[i][j] = str1[j];. At that time i might be way more than sum.
better solution but output is not that as required :
{ char str1[20];
cin.get(str1, 20);
int len, sum = 0;
len = strlen(str1);
int i = 0;
while (str1[i] != '\0')
{
if (str1[i] == ' ' || str1[i] == '.' || str1[i] == ',' || str1[i] == '?' || str1[i] == ';')
{
sum = sum + 1;
}
i++;
}
char **ptr;
ptr = new char*[sum];
for (int i = 0; i < sum; i++)
{
ptr[i] = new char[len];
}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < len; j++)
{
if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
{
ptr[i][j] = str1[j];
i++;
}
else
{
ptr[i][j] = str1[j];
}
}
}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < len; j++)
{
cout << ptr[i][j];
}
cout << endl;
}
for (int i = 0; i < sum; i++)
{
delete[] ptr[i];
}
delete[] ptr;
system("pause");
return 0;}
We were given an assignment to make a program that takes two decimal numbers as input and performs binary addition in the background then outputs a decimal sum. I seem to be getting the wrong results despite how much I think my code logic is correct.
My current sum gives me 0. The Correct answer should be 24.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int add(int, int);
int decbin(int);
int bindec(long);
int binADD(string, string);
int main()
{
int a = 15;
int b = 9;
int z;
z = add(a,b);
cout << z;
return 0;
}
int add(int a, int b)
{
int z;
ostringstream x, y;
x << decbin(a);
y << decbin(b);
z = binADD(x.str(), y.str());
return z;
}
int decbin(int x)
{
int d[16];
int i = 0;
int j;
int ans;
while(x > 0)
{
d[i] = x % 2;
i++;
x = x / 2;
}
for(j = i - 1; j >= 0; j--)
{
ans = d[j];
}
return ans;
}
int bindec(int x)
{
int bin, dec = 0, rem, base = 1;
bin = x;
while (x > 0)
{
rem = x % 10;
dec = dec + rem * base;
base = base * 2;
x = x / 10;
}
return dec;
}
int binADD(string a, string b)
{
int carry = 0;
int result[5];
int res;
int ans;
for(int i = 0; i < 4; i++)
{
if(a[i] == '1' && b[i] == '1' && carry == 0)
{
result[i] = 0;
carry = 1;
}
else if(a[i] == '0' && b[i] == '1' && carry == 1)
{
result[i] = 0;
carry = 1;
}
else if(a[i] == '1' && b[i] == '1' && carry == 1)
{
result[i] = 1;
carry = 1;
}
else if(a[i] == '1' && b[i] == '0' && carry == 1)
{
result[i] = 0;
carry = 1;
}
else if(a[i] == '1' && b[i] == '0' && carry == 0)
{
result[i] = 1;
carry = 0;
}
else if(a[i] == '0' && b[i] == '0' && carry == 1)
{
result[i] = 1;
carry = 0;
}
else if(a[i] == '0' && b[i] == '1' && carry == 0)
{
result[i] = 1;
carry = 0;
}
else if(a[i] == '0' && b[i] == '0' && carry == 0)
{
result[i] = 0;
carry = 0;
}
}
result[4] = carry;
for(int j = 4; j >= 0; j--)
{
res = result[j];
}
ans = bindec(res);
return ans;
}