http://acm.pku.edu.cn/JudgeOnline/problem?id=1154
Код:
#include <iostream> #include <list> #include <cstring> using namespace std; const int len=sizeof(char)*26; struct NODE { int row,col,step; bool v[26]; }; int d[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; list<NODE>p; NODE temp,t; int main() { int row,col,i,best; char map[25][25],ch; scanf("%d%d\n",&row,&col); for(i=0;i<row;i++) gets(map[i]); temp.row=0; temp.col=0; temp.step=1; memset(temp.v,0,len); ch=map[0][0]; temp.v[ch-'A']=true; p.push_back(temp); best=1; while(!p.empty()) { temp=p.front(); p.pop_front(); if(temp.step>best) best=temp.step; for(i=0;i<4;i++) { t.row=temp.row+d[i][0]; t.col=temp.col+d[i][1]; if(t.row>=0 && t.row<row && t.col>=0 && t.col<col) { ch=map[t.row][t.col]; if(!temp.v[ch-'A']) { memcpy(t.v,temp.v,len); t.v[ch-'A']=true; t.step=temp.step+1; p.push_back(t); } } } } printf("%d\n",best); return 0; }