## 10500 - Robot maps

Moderator: Board moderators

slxst
New poster
Posts: 23
Joined: Mon Oct 16, 2006 2:18 am
It seems that the answer is OK but I have problems with the presentation. I have tried every thing that I can think of and I still got WA.

Can anyone help me?

Thanks.

Code: Select all

``````#include <iostream>
#include <vector>
#include <string>
using namespace std;

ostream& operator<<(ostream& out, vector<string>& v)
{
int s = v[0].size();
string l("|---");
for(int times=1; times<s; ++times)
l += "|---";

for(int i=0; i<v.size(); ++i)
{
out << l << '|' << endl;
for(int j=0; j<v[0].size(); ++j)
{
out << "| " << v[i][j] << ' ';
}
out << '|' << endl;
}
out << l << '|' << endl;

return out;
}

int move(vector<string>& site, vector<string>& map, int x, int y)
{
bool done=false;
int m = 0;
while(!done)
{
site[x][y] = '*';

if(x-1>=0)
if(site[x-1][y]!='*')
map[x-1][y] = site[x-1][y];
if(y-1>=0)
if(site[x][y-1]!='*')
map[x][y-1] = site[x][y-1];
if(x+1<map.size())
if(site[x+1][y]!='*')
map[x+1][y] = site[x+1][y];
if(y+1<map[0].size())
if(site[x][y+1]!='*')
map[x][y+1] = site[x][y+1];

int h[] = {-1, 0, 1, 0};
int v[] = { 0, 1, 0,-1};

bool valid = false;

for(int i=0; i<4 && !valid; ++i)
{
if(x+h[i]>=0 && x+h[i]<map.size() && y+v[i]>=0 && y+v[i]<map[0].size())
{
if(site[x+h[i]][y+v[i]]!='X' && site[x+h[i]][y+v[i]]!='*')
{
x += h[i];
y += v[i];
valid = true;
++m;
}
}
}

done = !valid;
}

return m;
}

int main(int argc, char** argv)
{
int w=0, h=0;
cin >> h >> w;

while(w!=0)
{
cout << endl;

int x=0, y=0;
cin >> x >> y;

vector<string> site(w, string(h,' '));
vector<string> map(w, string(h,'?'));

for(int i=0; i<w; ++i)
{
for(int j=0; j<h; ++j)
{
cin >> site[i][j];
}
}
map[x-1][y-1] = site[x-1][y-1];

int m = move(site, map, x-1, y-1);
cout << map << endl << "NUMBER OF MOVEMENTS: " << m << endl;

cin >> w >> h;
}

return 0;
}``````