#include <iostream>
#include <cstring>
#include <cmath>
#include <sstream>
using namespace std;
int main()
{
//QLGN AEKI RLRN GEAE
stringstream out;
string str1 = "QLGNAEKIRLRNGEAE";
string str2 = "KING";
int lenght = sqrt(str1.size());
char arr[lenght][lenght];
char buf1[lenght];
char buf2[str2.size()];
strcpy(buf2, str2.c_str());
strcpy(buf1, str1.c_str());
for(int i = 0, k = 0; i < lenght; ++i)
{
for(int j = 0; j < lenght; ++j)
{
arr[i][j] = buf1[k++];
cout << arr[i][j] << " ";
}
cout << endl;
}
for(size_t i = 0; i <= str2.size(); i++)
{
for(int rows = 0; rows < lenght; rows++)
{
for(int cols = 0; cols < lenght; cols++)
{
if((buf2[i] == arr[rows][cols])
&& ( (buf2[i+1] == arr[rows][cols+1]) ||
(buf2[i+1] == arr[rows][cols-1] ||
(buf2[i+1] == arr[rows-1][cols] ||
(buf2[i+1] == arr[rows+1][cols])))))
{
if(rows > lenght) { rows++; cols = 0; }
if(rows < 0) { rows--; cols = lenght; }
if(cols > lenght) { cols++; rows = 0; }
if(cols < 0) { cols--; rows = lenght; }
if(rows > lenght && cols > lenght) { rows = 0; cols = 0; }
if(rows < 0 && cols < 0) { rows = lenght; cols = lenght; }
out << buf2[i] << "[" << rows << ',' << cols << "]->";
i++;
rows = 0; cols = 0;
}
}
}
}
string str = out.str();
string::size_type size = str.size();
if(size > 0) str.erase(size - 2, 2);
cout << endl << str << endl;
return 0;
}