Chessboard pattern

Hello. This is my task: https://judge.softuni.bg/Contests/Practice/DownloadResource/599
I've already made the spiral part.
I need help with the loop for the chessboard walk.
did you filled the matrix? what is your code.
notice that, for even sized matrix, top left corner is white, and black for odd sized matrix.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void fill_spiral(const int &size, char a[100][100], const string &line)
{
	int i, k = 0, l = 0;
	int n = size;
	int m = size;
	int stringlength = 0;
	while (k < m && l < n)
	{
		
		for (i = l; i < n; ++i)
		{
			a[k][i]=line[stringlength];
			stringlength++;
		}
		k++;

		for (i = k; i < m; ++i)
		{
			a[i][n - 1]=line[stringlength];
			stringlength++;
		}
		n--;

	
		if (k < m)
		{
			for (i = n - 1; i >= l; --i)
			{
				a[m - 1][i] = line[stringlength];
				stringlength++;
			}
			m--;
		}

	
		if (l < n)
		{
			for (i = m - 1; i >= k; --i)
			{
			     a[i][l]=line[stringlength];
				 stringlength++;
			}
			l++;
		}
	}
}
Last edited on
i think the code is ok. but, as nothing is specified about maximum size in the question, you should use dynamic 2d array.

e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(){
	
	int size= 4;	
	string s= "Rvioes roi tset";
	// cin >> size; 
	//getline(cin,s);
	vector<char> v(size, ' ');
	vector<vector<char>> vv(size, v);	
	
}

i think the code is ok.

I still don't get why you asked me.
I need an algorithm for the chess walk.
I need an algorithm for the chess walk.

Looking at the requirements for the problem, what would you do on paper? How would you translate that to code?
mighty asker wrote:
I still don't get why you asked me.
I need an algorithm for the chess walk.


i wanted to make sure you did that correctly. 'cause its the main challenge of the problem. the remaining part is easier :)

the algorithm is similar to summing up all numbers of a int matrix.
you need two passes, sum up them to two std::string.
what would you do on paper? How would you translate that to code?

If I had an idea, I'd probably have written the code.

the remaining part is easier :)
the algorithm is similar to summing up all numbers of a int matrix.

I'm not sure.
Actually...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
string result;
	string second;
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size; j++)
		{
			if (j%2==0)
		    {
				result += arr[i][j];
			}
		}
		i++;
		for (int j = 0; j < size; j++)
		{
			if (j % 2 != 0)
			{
				result += arr[i][j];
			}
		}
	}

	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size; j++)
		{
			if (j % 2 != 0)
			{
				second += arr[i][j];
			}
		}
		i++;
		for (int j = 0; j < size; j++)
		{
			if (j % 2 == 0)
			{
				second += arr[i][j];
			}
		}
	}
	result += second;

There must be a better way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void fill_spiral(const int &size,  const string &line, vector<vector<char>> &a);

int main(){
	
	int size= 4;	
	string s= "Rvioes roi tset";
	
	vector<char> v(size, ' ');
	vector<vector<char>> vv(size, v);		
	fill_spiral(size, s, vv);
	
	///
	string s0="", s1="";
	for(int i=0; i<size; i++){
		for(int j=0; j<size; j++){
			if(i%2==0){
				if(j%2==0) s0+= vv[i][j];
				else  s1+= vv[i][j];
			}
			else{
				if(j%2==0) s1+= vv[i][j];
				else  s0+= vv[i][j];
			}
		}					
	}
	
	cout << "\"" << s0 << "\" + \"" << s1 << "\"\n";
	///
}


void fill_spiral(const int &size,  const string &line, vector<vector<char>> &a) // same as OPs
{
	//char a[100][100]; ///
	int i, k = 0, l = 0;
	int n = size;
	int m = size;
	int stringlength = 0;
	while (k < m && l < n)
	{		
		for (i = l; i < n; ++i)
		{
			a[k][i]=line[stringlength];
			stringlength++;
		}
		k++;
		for (i = k; i < m; ++i)
		{
			a[i][n - 1]=line[stringlength];
			stringlength++;
		}
		n--;	
		if (k < m)
		{
			for (i = n - 1; i >= l; --i)
			{
				a[m - 1][i] = line[stringlength];
				stringlength++;
			}
			m--;
		}	
		if (l < n)
		{
			for (i = m - 1; i >= k; --i)
			{
			     a[i][l]=line[stringlength];
				 stringlength++;
			}
			l++;
		}
	}
	/// test print
		for(int i=0; i<size; i++){
			for(int j=0; j<size; j++){
				cout << a[i][j] << " ";
			}	
			cout << endl;
		}
		cout << "\n";
	///
}
If you follow the link, the problem says that size is in the range [1,9], so a vector of vectors isn't necessary.

I didn't try running it, but it appears to me that your fillSpiral() function doesn't check the length of the string. In any case, here's another way to do it. This uses a recursive function that fills the edge of a spiral and recurses to fill the inner part.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void
fillSquare(const string &str, Board &board, size_t pos, int startR, int startC, int size)
{
    int r = startR, c = startC;
    int i;

    if (size == 1) {
	board[r][c] = str[pos];
	return;
    }

    // Fill the square from the top left corner going clockwise.
    // each loop below does one side
    --size;
    for (i=0; i<size && pos < str.size(); ++i) { // top
	board[r][c++] = str[pos++];
    }
    for (i=0; i<size && pos < str.size(); ++i) { // right
	board[r++][c] = str[pos++];
    }
    for (i=0; i<size && pos < str.size(); ++i) { // bottom
	board[r][c--] = str[pos++];
    }
    for (i=0; i<size && pos < str.size(); ++i) { // left
	board[r--][c] = str[pos++];
    }
    if (pos < str.size()) {
	fillSquare(str, board, pos, startR+1, startC+1, size-1);
    }
}


void
fillSpiral(const string &str, Board &board, int size)
{
    fillSquare(str, board, 0, 0, 0, size);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(){	
	int size= 4;	
	string s= "Rvioes roi tset";
	if(s.size() > size*size) exit (-1);
	while(s.size() < size*size)	s+= " ";
	vector<vector<char>> a(size, vector<char>(size,' '));	 	
	int x=0, y=-1, rem =size, index=0;
	while(1){	
		for(int i=0; i<rem; i++) a[x][++y]= s[index++];		
		--rem;	if(!rem) break;	
		for(int i=0; i<rem; i++) a[++x][y]= s[index++];		
		for(int i=0; i<rem; i++) a[x][--y]= s[index++];		
		--rem; if(!rem) break;
		for(int i=0; i<rem; i++) a[--x][y]= s[index++];			
	}	
	string s0="", s1="";
	for(int i=0; i<size; i++){
		for(int j=0; j<size; j++){
			if(i%2==0){
				if(j%2==0) s0+= a[i][j];
				else  s1+= a[i][j];
			}
			else{
				if(j%2==0) s1+= a[i][j];
				else  s0+= a[i][j];
			}
		}					
	}	
	cout << "\"" << s0 << "\" + \"" << s1 << "\"\n";
}
Topic archived. No new replies allowed.