Pagini recente » Cod sursa (job #584207) | Cod sursa (job #2251368) | Cod sursa (job #165369) | Cod sursa (job #1037606) | Cod sursa (job #1141433)
#include<fstream>
#include<iostream>
#include<queue>
using namespace std;
int n, m;
char h[101][101];
int hr[101][101], hj[101][101];
int di[] = { -1, 0, 1, 0, 1, 1, -1, -1 };
int dj[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
struct punct
{
int i, j;
}rom,jul;
queue<punct> que;
punct road[101][101];
ifstream f("rj.in");
ofstream g("rj.out");
void lee(punct start, punct finish, int harta[][101])
{
while (!que.empty())
que.pop();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
harta[i][j] = -1;
}
que.push(start);
road[start.i][start.j] = start;
while (!que.empty())
{
punct aici = que.front();
for (int i = 0; i < 8; i++)
{
int new_i = aici.i + di[i];
int new_j = aici.j + dj[i];
if (new_i >= 0 && new_j >= 0 && new_i < n&&new_j<m&& h[new_i][new_j] == ' ' && ((harta[new_i][new_j]>harta[aici.i][aici.j] + 1) || (harta[new_i][new_j] == -1)))
{
harta[new_i][new_j] = harta[aici.i][aici.j] + 1;
punct b;
b.i = new_i;
b.j = new_j;
punct c;
c.i = aici.i;
c.j = aici.j;
road[new_i][new_j] = c;
que.push(b);
}
}
que.pop();
}
}
void scrie(int i,int j,punct start)
{
punct v[101];
int ok = 1;
int c = 0;
int in, jn;
while (ok)
{
if (road[i][j].i == start.i&&road[i][j].j == start.j)
{
ok = 0;
}
v[c++] = road[i][j];
in = road[i][j].i;
jn = road[i][j].j;
i = in;
j = jn;
}
//for (int i = 0; i < c; i++)
// cout << v[i].i+1 << "-" << v[i].j+1 << " ";
g << c - (c - 1) / 2<<" "<< v[(c - 1) / 2].i+1 << " " << v[(c - 1) / 2].j+1;
}
int main()
{
f >> n >> m;
f.get();
char lin[101];
for (int i = 0; i < n; i++)
{
f.getline(lin,101);
for (int j = 0; j < m; j++)
{
h[i][j] = lin[j];
if (lin[j] == 'R')
{
rom.i = i;
rom.j = j;
h[i][j] = ' ';
}
if (lin[j] == 'J')
{
jul.i = i;
jul.j = j;
h[i][j] = ' ';
}
}
}
lee(rom, jul, hr);
/*for (int i = 0; i <= n; i++)
{
for (int j = 0; j < m; j++)
cout << h[i][j];
cout << endl;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cout << hr[i][j] << " ";
cout << endl;
}*/
scrie(jul.i,jul.j,rom);
//system("pause");
f.close();
g.close();
return 0;
}