Wersja C ++ (610 607 606 584)
#include<queue>
#include<set>
#include<string>
#include<iostream>
#include<memory>
#define S second
#define X s.S.first
#define Y s.S.S
#define A(x,y) f.push(make_pair(s.first-c,make_pair(X+x,Y+y)));
#define T typedef pair<int
using namespace std;T,int>P;T,P>Q;string l;vector<string>b;priority_queue<Q>f;set<P>g;Q s;int m,n,c=0;int main(){cin>>m>>n;getline(cin,l);while(getline(cin,l))b.push_back(l);A(0,0)while(!f.empty()){s=f.top();f.pop();if(X>=0&&X<=m&&Y>=0&&Y<=n&&g.find(s.S)==g.end()){g.insert(s.S);c=b[X][Y]=='x';if(X==m&&Y==n)cout<<-(s.first-c);A(1,0)A(-1,0)A(0,1)A(0,-1)}}}
Implementuje algorytm Dijkstry.
Bez golfa:
#include<queue>
#include<set>
#include<string>
#include<iostream>
#include<memory>
using namespace std;
typedef pair<int,int>P;
typedef pair<int,P>Q;
int main()
{
int m,n;
string line;
vector<string> board;
cin >> m >> n;getline(cin,l);
while(getline(cin,line))
{
board.push_back(line);
}
priority_queue<Q> frontList;
set<P> found;
frontList.push(make_pair(0,make_pair(0,0)));
while(!frontList.empty())
{
Q s=frontList.top();
frontList.pop();
if( s.second.first>=0
&& s.second.first<=m
&& s.second.second>=0
&& s.second.second<=n
&& found.find(s.second)==found.end()
)
{
found.insert(s.second);
int c=board[s.second.first][s.second.second]=='x';
if( s.second.first==m
&& s.second.second==n
)
{ cout<<-(s.first-c);
}
frontList.push(make_pair(s.first-c,make_pair(s.second.first+1,s.second.second)));
frontList.push(make_pair(s.first-c,make_pair(s.second.first-1,s.second.second)));
frontList.push(make_pair(s.first-c,make_pair(s.second.first,s.second.second+1)));
frontList.push(make_pair(s.first-c,make_pair(s.second.first,s.second.second-1)));
}
}
}