# include <iostream>
usingnamespace std;
int main ()
{
char word [10][10] ;
int helper [10][10] , set = 0 ; // helper to number the grid
int row , col ;
while (cin >> row)
{
set ++ ;
int counter = 0 ;
if (row == 0) // checking if the input is zero
{
break;
}
cin >> col ;
if (col == 0)
{
break;
}
// reading the grid elements and numbering the first row in helper
for (int i=0 ; i<row ; i++)
{
for (int j=0 ; j<col ; j++)
{
cin >> word [i][j] ;
if (word [i][j] != '*' && i == 0)
{
counter++;
helper [i][j] = counter ;
}
}
}
// these rules to number the rest of the grid in helper
for (int i=1 ; i<row ; i++)
{
for (int j=0 ; j<col ; j++)
{
if (word [i][j] == '*' && word [i][j+1] != '*' && j+1 < col )
{
counter++ ;
helper [i][j+1] = counter ;
}
if ((word [i][j] != '*' && word [i-1][j] == '*' && word [i][j-1] != '*') || (word [i][j] != '*' && j==0 ))
{
counter++ ;
helper [i][j] = counter ;
}
}
}
// printing the output for Across
cout << "puzzle #"<< set << ":" << endl << "Across" << endl;
for (int i=0 ; i<row ; i++)
{
for (int j = 0 ; j<col ; j++)
{
if (word [i][j] != '*')
{
cout << helper [i][j] << "." << word [i][j] ;
j++ ;
while (word [i][j] != '*' && j<col)
{
cout << word [i][j] ;
j++ ;
}
cout << endl;
}
}
}
// printing the output for Down (printing the letters in each column till it encounters a black square , then it loops the next column)
cout << "Down" << endl;
for (int i=0 ; i<col ; i++)
{
for (int j=0 ; j<row ; j++)
{
if (word [j][i] != '*')
{
int y = j ; // to maintain the j increment
cout << helper [y][i] << "." << word [y][i] ;
y++ ;
while (word [y][i] != '*' && y<row)
{
cout << word [y][i] ;
y++ ;
}
cout << endl;
break;
}
if (word [j][i] == '*' && word [j+1][i] != '*')
{
int y = j+1 ; // to maintain the j increment
cout << helper [y][i] << "." << word [y][i] ;
y++ ;
while (word [y][i] != '*' && y<row)
{
cout << word [y][i];
y++ ;
}
cout << endl;
break;
}
}
}
// printing the rest of the Down by looping from the the second row.....and so on
for (int i=1 ; i<row ; i++)
{
for (int j=0 ; j<col ; j++)
{
if ((word [i][j] == '*' && word [i+1][j] != '*' && i+1<row ))
{
int x = i ;
x ++ ;
cout << helper [x][j] << "." << word [x][j] ;
x++ ;
while (word [x][j] != '*' && x<row)
{
cout << word [x][j] ;
x++ ;
}
cout << endl;
}
}
}
cout << endl;
}
}
cout << "Down" << endl;
int down [10] ;
for (int i=0 ; i<col ; i++)
{
for (int j = 0 ; j<row ; j++)
{
if (word [j][i] != '*')
{
cout << helper [j][i] << "." << word [j][i] ;
j++ ;
while (word [j][i] != '*' && j<row)
{
cout << word [j][i] ;
j++ ;
}
cout << endl;
}
break;
}
}
What's line 75 supposed to do? Anyway, when you output the "Down" words, you are skipping the rest of the row.
For example, in the second example given, the word "IMPOSE" is from row 1 to row 6. Your code will go from j = 0 to j = 5 when you output it, which means you will skip words like "DO", "ENTIRE", "ROD", etc.
You are outputting the down words in incorrect order. You are outputting from top to bottom, then from left to right. The numbering scheme is from left to right, then top to bottom.
Also, maybe you should code it to take input from a file instead of the console.
but here is sample input :
6 7
AIM*DEN
*ME*ONE
UPON*TO
SO*ERIN
*SA*OR*
IES*DEA
output is : Across
1.AIM
4.DEN
7.ME
8.ONE
9.UPON
11.TO
12.SO
13.ERIN
15.SA
17.OR
18.IES
19.DEA Down
1.A
2.IMPOSE
3.MEO
4.DO
5.ENTIRE
6.NEON
9.US
10.NE
14.ROD
16.AS
18.I
20.A
this is how I understand Across and down : Across : output should be form left to right (in the same row) if a block is encountered , skip it and finish the row ....then proceed to the next row and so on....
Down : output should be from top to down (in the same column) if a block is encountered , then proceed to the next column and so on.....when the column are finished , check again by starting from the second row ....and so on
My mistake. I didn't notice the break; in the code I pointed out. That seems to make a very inefficient loop though, with you needing to add a whole other loop after it to print out the rest of the "Down" words.
Try changing line 41 to this: if (j+1 < col && word [i][j] == '*' && word [i][j+1] != '*')
That should take care of the out of bounds problem ne555 pointed out.
If that doesn't fix it, maybe it's because you assume the puzzle will be a maximum size of 10x10. You can try changing the code to use dynamic memory allocation.
Thanks a lot at first
commenting on nes55
I don't understand how did the counter reach 3
I debugged it I did not even see number 2 in the whole grid.......
then how did the counter reach number 3 without reaching number 2
and one more thing why is the for loop in line 36 go out of bounds
I don't understand how ?
consider this input case :
4 3
a e *
m * l
* n o
* m n
the output according to UVA toolkit is :
puzzle #1: Across
1.ae
3.m
4.l
5.no
6.mn Down
1.am
2.e
4.lon
5.nm
if this is certainly true , then I don't understand the output
here try it for yourself here : http://uvatoolkit.com/problemssolve.php
please write the word cross in the search bar and choose crossword answers......
I must understand the output in order to accept this problem
and here an other sample input :
3 3
a e *
m * l
* n o
according to uva toolkit the output is :
puzzle #1: Across
1.ae
3.m
4.l
5.no Down
1.am (I agree with this)
2.e (I agree with this)
4.lo (I don't agree with this)
5.n (I don't agree with this)
Well, I tried submitting it and looks like mine was the wrong answer as well. (Also, apparently you must use console input because file input results in a runtime error.)