so I tried to recreate Conways Game of Life and i got it pretty much working but i have numbers in my output and i have no idea where they are coming from.
Im fairly new to c++and programming in general i learned python and java before but i have never seen this and google didnt seem to understand my question so i hope you fellow humans do.
My output looks like this:
1664447571170186994045474010000255652800 /its about this number
0000000000
0011000000
0110000000
0001000000
0000000000
0000000000
0000000000
0000000000
0000000000
anyone has any idea where they come from cause i certainly don't.
sometimes these numbers even "collide" with the board like this:
0000-1-1-1-116644475711701869940
4547401010000000
791621423110000000
0101000000
0000000000
0000000000
0000000000
0000000000
0000000000
0020000000
My sphagetthi code:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
int main(){
int x = 10, y = 10, i, j, c, b;
int field[y][x] =
{{ 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},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,1,1,0,0,0,0},
{ 0,0,0,0,1,0,1,0,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,0,0,0,0,0}};
int updateField[y][x];
cout << "start";
for(b = 0;b<=10; b++){
cout << "main loop: "<<b<<"\n";
for(y = 0; y < 10; y++)
{
for(x = 0; x < 10; x++)
{
c = 0;
if(y == 0 || y == 9 || x == 0 || x == 9){
}
else{
/*v v v lebende zelle v v v*/
if(field[y][x] == 1){
if(field[y-1][x-1] == 1){
c++;
}
if(field[y-1][x] == 1){
c++;
}
if(field[y-1][x+1] == 1){
c++;
}
if(field[y][x-1] == 1){
c++;
}
if(field[y+1][x-1] == 1){
c++;
}
if(field[y+1][x] == 1){
c++;
}
if(field[y+1][x+1] == 1){
c++;
}
if(field[y][x+1] == 1){
c++;
}
/*regeln v v v*/
if(c < 2 or c > 3){
updateField[y][x] = 0;
}
else
{
updateField[y][x] = 1;
}
}
/*^ ^ ^ lebende zelle ^ ^ ^*/
/*v v v tote zelle v v v*/
else if(field[y][x] == 0){
if(field[y-1][x-1] == 1){
c++;
}
if(field[y-1][x] == 1){
c++;
}
if(field[y-1][x+1] == 1){
c++;
}
if(field[y][x-1] == 1){
c++;
}
if(field[y+1][x-1] == 1){
c++;
}
if(field[y+1][x] == 1){
c++;
}
if(field[y+1][x+1] == 1){
c++;
}
if(field[y][x+1] == 1){
c++;
}
/*regeln v v v*/
if(c == 3)
{
updateField[y][x] = 1;
}
else
{
updateField[y][x] = 0;
}
}
}
}
}
cout << "\n";
for(i = 0; i < 10; i++){
for(j = 0; j < 10; j++){
field[i][j] = updateField[i][j];
}
}
for(i = 0;i < x; i++){
for(j = 0; j < y; j++ )
{
cout << field[i][j];
}
cout << "\n";
}
sleep_for(nanoseconds(500000000));
}
}
I'd appreciate any answer. thanks in advance.
The problem is that you ignore the borders when building updateField (0 or 9 in a component) but then copy updateField including those borders into field. The borders in updateField were never set, so they just contain any number which was in the memory before which is not controlled by your program.
You should either set the borders of updateField to zero or do not copy the borders into field iterating from 1 to excluding 9 instead of from 0 to 10.
While I hope that this solves your problem I would also like to share some ideas how to refactor your code. For instance you could use arrays of boolean values instead of integers as the values clearly can only be 0 or 1 according to the games logic. Scanning the neighborhood of a cell also adds a lot of redundant code which can be made shorter. Just see the following as a general idea about some improvements which can be done:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
int main(){
int x = 10, y = 10, i, j, c, b;
bool field[y][x] =
{{ 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},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,1,1,0,0,0,0},
{ 0,0,0,0,1,0,1,0,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,0,0,0,0,0}};//too lazy to rewrite to true/false, should also work this way
bool updateField[y][x];
cout << "start";
for(b = 0;b<=10; b++){
cout << "main loop: "<<b<<"\n";
for(y = 0; y < 10; y++)
{
for(x = 0; x < 10; x++)
{
c = 0;
if(y == 0 || y == 9 || x == 0 || x == 9){
updateField[y][x] = false;
}
else{
for(i=-1;i<2;i++){
for(j=-1;j<2;j++){
if((i != 0 || j != 0) && field[y+i][x+j] == 1){// exclude i == j == 0
c++;
}
}
}
if(field[y][x]){
if(c < 2 or c > 3){
updateField[y][x] = false;
}
else
{
updateField[y][x] = true;
}
}else{
if(c == 3)
{
updateField[y][x] = true;
}
else
{
updateField[y][x] = false;
}
}
}
}
}
cout << "\n";
for(i = 0; i < 10; i++){
for(j = 0; j < 10; j++){
field[i][j] = updateField[i][j];
}
}
for(i = 0;i < x; i++){
for(j = 0; j < y; j++ )
{
cout << field[i][j];
}
cout << "\n";
}
sleep_for(nanoseconds(500000000));
}
}
If you initialize updateField as well, the weird numbers will be gone.
Because of this if statement, the edge of updatefield doesn't update:
if (y == 0 || y == 9 || x == 0 || x == 9) ;
else {
///long statements
if (c < 2 || c > 3) {
updateField[y][x] = 0;
}
else
{
updateField[y][x] = 1;
}
}
Also, you should be more consistent with your usage of variables, for example you define your map to be x*y sized, but later in the for loops you specify the maximum value of x and y to be 10.
What you're seeing is the contents of uninitialized memory. When you declare int updateField[y][x]; the compiler reserves an appropriate space of memory for you, but it doesn't initialize the memory for you, so it contains whatever random junk was already there.
To fix this, initialize updateField to be identical to field before running the loop:
for (int i = 0; i < y; ++i) {
for (int j = 0; j < x; ++j) {
updateField[i][j] = field[i][j];
}
}
Related
enter image description hereThe program outputs the number of the leftmost column with only positive numbers. Everything works fine in the Visual Studio Code terminal.
I think double-clicking on the automatically generated .exe file in a separate window should launch a full-fledged program that reads the input, processes it and displays the output (the number of the desired column or a message that there is none). In fact, when running this .exe file, a window appears, I can enter 12 numbers through Enter, but the window disappears after entering the 12th number.
Am I correct in my assumption, and if so, what could be causing the problem? If not, why is this file needed at all?
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n = 3, m = 4;
int matrix[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> matrix[i][j];
}
};
int col1[4];
int col2[4];
int col3[4];
int col4[4];
//создание 4 массивов-столбцов (creating 4 column arrays)
for (int l = 0; l < n; l++)
{
for (int k = 0; k < m; k++)
{
if ((k % 4) == 0)
{
col1[l] = matrix[l][k];
}
else if (((k - 1) % 4) == 0)
{
col2[l] = matrix[l][k];
}
else if (((k - 2) % 4) == 0)
{
col3[l] = matrix[l][k];
}
else if (((k - 3) % 4) == 0)
{
col4[l] = matrix[l][k];
}
};
};
//поиск крайнего левого столбца только положительных чисел (finding the leftmost column of only positive numbers)
int c = 0, s = 0;
for (int g = 0; g < m; g++)
{
if (g == 0)
{
for (int d = 0; d < n; d++)
{
if (col1[d] <= 0)
{
c++;
}
};
if (c == 0)
{
s++;
cout << "col 1";
break;
};
c = 0;
}
else if (g == 1)
{
for (int d = 0; d < n; d++)
{
if (col2[d] <= 0)
{
c++;
}
};
if (c == 0)
{
s++;
cout << "col 2";
break;
};
c = 0;
}
else if (g == 2)
{
for (int d = 0; d < n; d++)
{
if (col3[d] <= 0)
{
c++;
}
};
if (c == 0)
{
s++;
cout << "col 3";
break;
};
c = 0;
}
else if (g == 3)
{
for (int d = 0; d < n; d++)
{
if (col4[d] <= 0)
{
c++;
}
};
if (c == 0)
{
s++;
cout << "col 4";
break;
};
c = 0;
}
};
if (s == 0)
{
cout << "No positive columns";
};
return 0;
}
enter image description here
If you are using Microsoft Windows, then the console window will disappear as soon as your program ends. If you don't want this to happen, then you can
run your program from the Windows command prompt cmd.exe instead of double-clicking it, or
add something to the end of your program that prevents it from closing immediately, such as the following code statement:
std::system( "pause" );
Note that you will have to #include <cstdlib> in order to use std::system.
I am trying to create a c++ unbeatable tic tac toe AI. after watching several videos on the topic i thought I had it all figured out. An error pops up on the screen saying "Expression: vector subscript out of range". I believe the error is coming from the availableMoves() function. however I do not know why.
The game itself works fine. any help would be appreciated.
#include <iostream>
#include <vector>
#include <ctime>
bool in(std::vector<int> v, int element)
{
for (int i = 0; i < v.size(); i++)
{
if (element == v[i])
{
return true;
}
}
return false;
}
class Board
{
private:
char board[3][3] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} };
public:
void displayBoard()
{
std::cout << "___________________" << std::endl;
for (int i = 0; i < 3; i++)
{
std::cout << "| ";
for (int j = 0; j < 3; j++)
{
std::cout << board[i][j] << " | ";
}
std::cout << std::endl;
}
std::cout << "___________________" << std::endl;
}
std::vector<int> availableMoves()
{
std::vector<int> moves;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] != 'X' && board[i][j] != 'O')
{
moves.push_back(i * 3 + j);
}
}
}
return moves;
}
void move(int choice, char mark)
{
int y = choice / 3;
int x = choice - y * 3;
board[y][x] = mark;
}
void revert(int choice)
{
int y = choice / 3;
int x = choice - y * 3;
board[y][x] = (char)choice + 48;
}
int checkWin()
{
for (int i = 0; i < 3; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
{
if (board[i][0] == 'X')
{
return 1;
}
else if (board[i][0] == 'O')
{
return -1;
}
}
}
for (int i = 0; i < 3; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
{
if (board[0][i] == 'X')
{
return 1;
}
else if (board[0][i] == 'O')
{
return -1;
}
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
{
if (board[0][0] == 'X')
{
return 1;
}
else if (board[0][0] == 'O')
{
return -1;
}
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0])
{
if (board[0][2] == 'X')
{
return 1;
}
else if (board[0][2] == 'O')
{
return -1;
}
}
return 0;
}
int evaluate()
{
return (checkWin() * -1) * (availableMoves().size() + 1);
}
Board& operator=(Board& b)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = b.board[i][j];
}
}
return (*this);
}
};
class TicTacToe
{
private:
Board board;
int turn;
int searches = 0;
public:
TicTacToe()
{
std::srand(time(0));
turn = std::rand() % 2;
}
int minimax(int depth, Board curBoard, bool is_max)
{
searches++;
if (depth == 0 || curBoard.checkWin() != 0)
{
return board.evaluate();
}
if (is_max)
{
int max_eval = -2147483647;
for (int i = 0; i < curBoard.availableMoves().size(); i++)
{
curBoard.move(curBoard.availableMoves()[i], 'O');
depth -= 1;
int eval = minimax(depth, curBoard, false);
curBoard.revert(curBoard.availableMoves()[i]);
if (eval > max_eval)
{
max_eval = eval;
}
}
return max_eval;
}
if (!is_max)
{
int min_eval = 2147483647;
for (int i = 0; i < curBoard.availableMoves().size(); i++)
{
curBoard.move(curBoard.availableMoves()[i], 'X');
depth -= 1;
int eval = minimax(depth, curBoard, true);
curBoard.revert(curBoard.availableMoves()[i]);
if (eval < min_eval)
{
min_eval = eval;
}
}
return min_eval;
}
}
void game()
{
while (board.checkWin() == 0 && board.availableMoves().size() != 0)
{
board.displayBoard();
if (turn % 2 == 0)
{
std::cout << std::endl;
int choice;
std::cout << "Enter Your Move: ";
std::cin >> choice;
choice -= 1;
while (!in(board.availableMoves(), choice))
{
std::cout << "Enter A Valid Move: ";
std::cin >> choice;
}
board.move(choice, 'X');
std::cout << std::endl;
turn++;
}
board.displayBoard();
if (board.checkWin() != 0)
{
break;
}
if (turn % 2 == 1)
{
int ai = minimax(9 - (turn % 2), board, true);
std::cout << searches;
std::cin.get();
turn++;
}
}
if (board.checkWin() == 1)
{
std::cout << "You Won" << std::endl;
}
else if (board.checkWin() == -1)
{
std::cout << "You Lost" << std::endl;
}
else
{
std::cout << "Tie" << std::endl;
}
std::cout << "Would You Like To Play Again Y/N: ";
char playAgain;
std::cin >> playAgain;
if (playAgain == 'Y')
{
Board newBoard;
board = newBoard;
game();
}
}
};
int main()
{
TicTacToe ticTacToe;
ticTacToe.game();
}
Do you know how to debug? If not, you should definitely learn this, it's pretty helpful. But here's some things I found out.
The problem is not in availableMoves(), but in minimax(), more precisely in line 215, where the program calls curBoard. revert(curBoard. availableMoves()[i]).
void revert(int choice)
{
int y = choice / 3;
int x = choice - y * 3;
board[y][x] = (char)choice + 48;
}
for (int i = 0; i < curBoard.availableMoves().size(); i++)
{
curBoard.move(curBoard.availableMoves()[i], 'X');
depth -= 1;
int eval = minimax(depth, curBoard, true);
curBoard.revert(curBoard.availableMoves()[i]);
if (eval < min_eval)
{
min_eval = eval;
}
}
The error happens in the function revert, but I am not sure why. Maybe availableMoves also returns something wrong. Variable i is permanently 0 in the for-loop. So it is possible that there is something wrong at position 0 of the vector moves, which revert cannot handle. Try debugging yourself, maybe you'll find the problem.
In one of the largest cities in Bytland, Bytesburg, construction of the second stage of the metro is underway. During the construction of the first stage, N stations were built that were not interconnected. According to the master plan, the metro in Bytesburg should consist of no more than two lines. each metro line is straight. The president of the company responsible for laying the lines wants to make sure that no more than two metro lines can be laid, so that all stations built lie on at least one of the two
Exaple 1
input:
6
0 1
1 1
2 1
0 2
1 3
2 2
output:
no
Example 2
input:
6
2 2
4 6
1 0
2 1
6 1
1 1
output:
yes
I wrote the code, but on the seventh test on the testing system, it gives the wrong answer. Help me please. This is my code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct line {
int k, b;
bool x = false;
long long c = 0;
};
int n;
vector<line> lines;
vector<pair<int, int>> p;
bool Is3PointsOnLine(pair<float, float> p1, pair<float, float> p2, pair<float, float> p3) {
return ((p3.first - p1.first)*(p2.second - p1.second) == (p3.second - p1.second)*(p2.first - p1.first));
}
bool PointIsOnLine(line l, int x, int y) {
if (!l.x) {
if (y == ((l.k * x) + l.b))
return true;
}
else {
if (x == l.b)
return true;
}
return false;
}
line f(pair<int, int> c1, pair<int, int> c2) {
int x1 = c1.first;
int y1 = c1.second;
int x2 = c2.first;
int y2 = c2.second;
line ans;
if (x2 != x1) {
ans.k = (y2 - y1) / (x2 - x1);//fix
ans.b = -(x1 * y2 - x1 * y1 - x2 * y1 + x1 * y1) / (x2 - x1);
ans.x = false;
ans.c = 0;
}
else {
ans.k = 0;
ans.b = x1;
ans.x = true;
ans.c = 0;
}
return ans;
}
int a() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != i) {
for (int k = 0; k < n; k++) {
if ((k != i) && (k != j) && (Is3PointsOnLine(p[i], p[j], p[k]))) {
lines.push_back(f(p[i], p[j]));
return 0;
}
}
}
}
}
}
int main() {
cin >> n;
p.resize(n);
vector<int> not_used;
for (int i = 0; i < n; i++)
cin >> p[i].first >> p[i].second;
if (n < 5) {
cout << "yes";
return 0;
}
else {
a();
if (lines.size() == 0) {
cout << "no";
return 0;
}
pair<int, int> e = { -8,-8 };
for (int i = 0; i < n; i++) {
if (PointIsOnLine(lines[0], p[i].first, p[i].second)) {
lines[0].c++;
}
else if (e.first == -8) {
e.first = i;
}
else if (e.second == -8) {
e.second = i;
lines.push_back(f(p[e.first], p[e.second]));
for (int j = 0; j <= i; j++) {
if (PointIsOnLine(lines[1], p[j].first, p[j].second)) {
lines[1].c++;
}
}
e = { -5,-5 };
}
else if (PointIsOnLine(lines[1], p[i].first, p[i].second)) {
lines[1].c++;
}
else {
cout << "no";
return 0;
}
}
if (lines[0].c+1 >= n && e.first != -2 && e.second == -8) {
cout << "yes";
return 0;
}
else if (lines[0].c + lines[1].c >= n) {
cout << "yes";
return 0;
}
else {
cout << "no";
return 0;
}
}
return 0;
}
Code v2
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct line {
pair<int, int> p1, p2;
long long c = 0;
};
int n;
vector<line> lines;
vector<pair<int, int>> p;
bool Is3PointsOnLine(pair<float, float> p1, pair<float, float> p2, pair<float, float> p3) {
return ((p3.first - p1.first)*(p2.second - p1.second) == (p3.second - p1.second)*(p2.first - p1.first));
}
//лежит ли точка на прямой
bool PointIsOnLine(line l, int x, int y) {
return Is3PointsOnLine(l.p1, l.p2, {x , y});
}
int a() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != i) {
for (int k = 0; k < n; k++) {
if ((k != i) && (k != j) && (Is3PointsOnLine(p[i], p[j], p[k]))) {
line sdafsadf;
sdafsadf.p1 = p[i]; sdafsadf.p2 = p[j];
lines.push_back(sdafsadf);
return 0;
}
}
}
}
}
}
int main() {
cin >> n;
p.resize(n);
vector<int> not_used;
for (int i = 0; i < n; i++)
cin >> p[i].first >> p[i].second;
if (n < 5) {
cout << "yes";
return 0;
}
else {
//ищем первую прямую
a();
if (lines.size() == 0) {
cout << "no";
return 0;
}
pair<int, int> e = { -8,-8 };
for (int i = 0; i < n; i++) {
if (PointIsOnLine(lines[0], p[i].first, p[i].second)) {
lines[0].c++;
}
else if (e.first == -8) {
e.first = i;
}
else if (e.second == -8) {
e.second = i;
line sdafsadf;
sdafsadf.p1 = p[e.first]; sdafsadf.p2 = p[e.second];
lines.push_back(sdafsadf);
for (int j = 0; j <= i; j++) {
if (PointIsOnLine(lines[1], p[j].first, p[j].second)) {
lines[1].c++;
}
}
e = { -5,-5 };
}
else if (PointIsOnLine(lines[1], p[i].first, p[i].second)) {
lines[1].c++;
}
else {
cout << "no";
return 0;
}
}
if (lines[0].c+1 >= n && e.first != -2 && e.second == -8) {
cout << "yes";
return 0;
}
else if ((lines[0].c + lines[1].c >= n) && (lines[0].p1 != lines[1].p1) && (lines[0].p1 != lines[1].p2) && (lines[0].p2 != lines[1].p1) && (lines[0].p2 != lines[1].p2)) {
cout << "yes";
return 0;
}
else {
cout << "no";
return 0;
}
}
return 0;
}
Your function f is horribly broken because it uses integer division. Then everything after that which relies on k and b, including PointIsOnLine, will give wrong results. Is3PointsOnLine seems ok, change your line struct to store two points, not slope+intercept, and then PointInOnLine can just call Is3PointsOnLine.
I'm amazed that you passed any non-trivial test cases with such a bug.
This also will not work
if (lines[0].c + lines[1].c >= n)
because you aren't testing that lines[1] isn't a duplicate of lines[0].
Also, you can reach lines[0].c + lines[1].c == n and still miss one station completely, if another station lies at the intersection of the lines and gets double-counted.
This problem which is similar to another that I solved here is giving me a wrong answer even though the algorithm works on the sample case.I have initialized all the variables this time and it works on a modified version of my previous algorithm.
#include <iostream>
int main() {
int n;
std::cin >> n;
int arr[n];
for (int i = 0; i <n ;++i) {
std::cin >> arr[i];
}
int four_count = 0, two_count = 0, three_long=0, one_long = 0 , max1_long = 0 ,max3_long = 0,a_depth = 0,max_depth = 0;
for (int i = 0; i < n; ++i) {
if (arr[i] == 3) {
if (arr[i+1] == 1) {
++a_depth;
if (a_depth > max_depth) {
max_depth = a_depth;
}
}
++four_count;
three_long += 2;
}
if (arr[i] == 1) {
if (arr[i+1] == 3) {
++a_depth;
if (a_depth > max_depth) {
max_depth = a_depth;
}
}
++two_count;
one_long += 2 ;
}
if (arr[i] == 2) {
if (arr[i+1] == 4 && i < n-1) {
--a_depth;
}
--two_count;
}
if (arr[i] == 4) {
if (arr[i+1] == 2 && i < n-1){
--a_depth;
}
--four_count;
}
if (four_count == 0 && two_count == 0) {
if (three_long >= one_long) {
if (three_long > max3_long) {
max3_long = three_long+one_long;
}
three_long = 0;
one_long = 0;
}
else {
if (one_long > max1_long) {
max1_long = one_long+three_long;
}
one_long = 0;
three_long = 0;
}
}
}
std::cout << max_depth*2 << " " << max1_long << " " << max3_long;
std::cout << "\n";
return 0;
}
Here is a link to the problem:
https://www.codechef.com/ZCOPRAC/problems/ZCO12003
In the below code:
for (int i = 0; i < n; ++i) {
if (arr[i] == 3) {
if (arr[i+1] == 1) {
when i reaches n-1, arr[i+1] becomes arr[n] resulting in an out-of-bounds memory access which will lead to undefined behaviour.
Let's say n is equal to 5. That means the array arr has the maximum index 4, because the first one is 0.
In your loop
for (int i = 0; i < n; ++i)
{ if (arr[i] == 3) {
if (arr[i+1] == 1) {
at some point i becomes n-1, so i == 4, then you try arr[i+1] meaning arr[5], which is out of bound.
Note that in the comment to P.Ws post, you tried if (arr[i+1] == 1 && i < n-1) to fix this. That won't work because there still is an arr[i+1] being executed. You could fix this by using
if(i < n-1) {
if(arr[i+1]) {
but that would mean an even deeper nesting of your ifs. You should probably rethink your approach to the given problem.
Edit: Are you sure you mean ++i and not i++?
There is the problem:
The first man 'g' (who starts first) has to reach the final box 'e', so that the second man 'l' (whenever he do) could't catch the first man. The men can go left, right, up, down or can stay.
For example:
Input:
6 7
RRRRRRR
R_e___R
R_____R
R_RRR_R
R_gRl_R
RRRRRRR
The answer is "YES" because there is the way (Left, Up, Up, Up, Right).
How this problem can be implemented?
I'm using BFS and DFS.
Here is my code
#include <iostream>
#include <algorithm>
#include <stack>
#include <math.h>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
const int MAX = 32;
char a[MAX][MAX];
int used[MAX][MAX], m1[MAX][MAX], m2[MAX][MAX];;
int movesx[8] = {-1, 1, 0, 0};
int movesy[8] = { 0, 0, -1, 1};
int n, m, c = 0, flag = 0;
struct pc {
int x, y;
};
pc li, ga, fi;
queue <pc> q;
void BFS1(pc v) {
pc from, to;
memset(m1,0,sizeof(m1)); m1[v.y][v.x] = 0;
memset(used, 0, sizeof(used));
q.push(v); used[v.y][v.x] = 1;
while(!q.empty())
{
from = q.front(); q.pop();
for(int i = 0; i < 4; ++i) {
int x = from.x + movesy[i], y = from.y + movesx[i];
if( (a[y][x] == ' ' || a[y][x] == 'g' ) && !used[y][x]) {
used[y][x] = 1;
m1[y][x] = m1[from.y][from.x] + 1;
pc temp;
temp.x = x;
temp.y = y;
q.push(temp);
}
}
}
}
void BFS2(pc v) {
pc from, to;
memset(m2,0,sizeof(m2)); m2[v.y][v.x] = 0;
memset(used, 0, sizeof(used));
q.push(v); used[v.y][v.x] = 1;
while(!q.empty())
{
from = q.front(); q.pop();
for(int i = 0; i < 4; ++i) {
int y = from.y + movesy[i], x = from.x + movesx[i];
if( (a[y][x] == ' ' || a[y][x] == 'l' ) && !used[y][x]) {
used[y][x] = 1;
m2[y][x] = m2[from.y][from.x] + 1;
pc temp;
temp.x = x;
temp.y = y;
q.push(temp);
}
}
}
}
void DFS(pc v) {
used[v.y][v.x] = 1;
for(int i = 0; i < 4; ++i) {
int x = v.x + movesx[i], y = v.y + movesy[i];
if(a[y][x] == 'e') {
c = 1;
flag = 1;
return;
}
if( (a[y][x] == ' ' ) && !used[y][x] && m2[y][x] < m1[y][x] && flag == 0 ) {
pc temp;
temp.x = x;
temp.y = y;
DFS(temp);
}
}
}
int main() {
c = 0, flag = 0;
memset(used, 0, sizeof(used));
memset(a, 'R', sizeof(a));
cin >> n >> m;
string s;
getline(cin, s);
for(int i = 0; i < n; ++i) {
getline(cin, s);
for(int j = 0; j < m; ++j) {
a[i][j] = s[j];
if(a[i][j] == 'g') {
ga.x = j;
ga.y = i;
}
else if(a[i][j] == 'l') {
li.x = j;
li.y = i;
}
else continue;
}
}
BFS1(li);
BFS2(ga);
memset(used, 0, sizeof(used));
DFS(ga);
if(c == 1) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
Here is the second code:
#include <iostream>
#include <algorithm>
#include <stack>
#include <math.h>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
const int MAX = 32;
char a[MAX][MAX];
int used[MAX][MAX], m1[MAX][MAX], m2[MAX][MAX];;
int an[1002][MAX][MAX];
int movesx[8] = {-1, 1, 0, 0, 0};
int movesy[8] = { 0, 0, -1, 1, 0};
int n, m, c = 0, flag = 0;
struct pc {
int x, y;
};
pc li, ga;
void functionD() {
for(int z = 1; z <= 1000; ++z) {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(an[z - 1][i][j] == 1) {
int x, y;
for(int k = 0; k < 5; ++k) {
x = j + movesx[k];
y = i + movesy[k];
if(x < m && y < n && x >= 0 && y >= 0) {
if(a[y][x] != 'R' && a[y][x] != 'e') {
an[z][y][x] = 1;
}
}
}
}
}
}
}
}
void DFS(pc v, int k) {
used[v.y][v.x] = 1;
for(int i = 0; i < 5; ++i) {
int x = v.x + movesx[i], y = v.y + movesy[i];
if(a[y][x] == 'e') {
c = 1;
flag = 1;
return;
}
if(an[k][y][x] == 0 && a[y][x] != 'R' && !used[y][x] && flag == 0 && k <= 1000) {
pc temp;
temp.x = x;
temp.y = y;
DFS(temp, k + 1);
}
}
}
int main() {
int nn; cin >> nn;
for(int z = 0; z < nn; ++z) {
c = 0, flag = 0;
memset(used, 0, sizeof(used));
memset(a, 'R', sizeof(a));
cin >> n >> m;
string s;
getline(cin, s);
for(int i = 0; i < n; ++i) {
getline(cin, s);
for(int j = 0; j < m; ++j) {
a[i][j] = s[j];
if(a[i][j] == 'g') {
ga.x = j;
ga.y = i;
}
else if(a[i][j] == 'l') {
li.x = j;
li.y = i;
}
}
}
an[0][li.y][li.x] = 1;
functionD();
DFS(ga, 1);
if(c == 1) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
}
EDIT (By Jarod42):
I found a tricky map which failed:
9 9
RRRRRRRRR
R...Rg..R
R.RlRRR.R
R.R...R.R
R.RRR.R.R
R.Re....R
R.R.RRR.R
R.......R
RRRRRRRRR
l cannot protect both accesses to e.
or even simpler
RRRRRRRRRR
R...RRRRRR
R.R...RRRR
RlReR...gR
R.R...RRRR
R...RRRRRR
RRRRRRRRRR
You have first to create a map distance from each accesses to e.
Then it is a minmax (or alpha-beta):
if g current position in one map-distance is less than l current position is same map-distance, g wins.
if l has less or equal distance in all maps distance, g loses.
else g has to use one of its valid map to reach the goal, l counters with its maps (or stands).
(Note: g has no reasons to stand as l may do the same and we are at the same point).
(Edit: Note: in provided link, it seems that the secure path has to be chosen statically, so the dynamic part (3rd bullet) is a loose for g)
There is no need for the DFS. Just test whether l can reach e before g can. If he can, then he can catch g, otherwise g wins.
(And beware of redundancy in your code; BFS1 and BFS 2 are almost identical, and could be combined into a single function.)
EDIT: The OP has added (a link to) new information: l cannot enter e.
The correction to this algorithm is obvious, if inelegant. Consider the rooms surrounding e; if there is one that g can reach before l, then g wins.
There may be other catches in the linked problem statement; the OP can state the problem he wants answered in the question itself. We don't like "link only" questions here.