Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 测试用例中的sokoban错误_C++_Game Development - Fatal编程技术网

C++ 测试用例中的sokoban错误

C++ 测试用例中的sokoban错误,c++,game-development,C++,Game Development,构造一个图,其中顶点是元组(Sx,Sy,Bx,By),描述Sokoban播放器(Sx,Sy)和长方体(Bx,By)的当前位置。记录到达每个位置的已知最小成本,并使用优先级队列进行BFS,以找到箱子到达目标单元格位置的最短路径。这是生成的代码。。。 输入格式 第一行由两个整数r和c组成(都是≤ 20) ,分别表示迷宫的行数和列数 下面是r行,每行包含c字符。每个字符描述迷宫的一个单元。满是岩石的单元格用“#”表示,空单元格用“.”表示。您的起始位置用“S”表示,方框的起始位置用“B”表示,目标单元

构造一个图,其中顶点是元组(Sx,Sy,Bx,By),描述Sokoban播放器(Sx,Sy)和长方体(Bx,By)的当前位置。记录到达每个位置的已知最小成本,并使用优先级队列进行BFS,以找到箱子到达目标单元格位置的最短路径。这是生成的代码。。。 输入格式

第一行由两个整数r和c组成(都是≤ 20) ,分别表示迷宫的行数和列数

下面是r行,每行包含c字符。每个字符描述迷宫的一个单元。满是岩石的单元格用“#”表示,空单元格用“.”表示。您的起始位置用“S”表示,方框的起始位置用“B”表示,目标单元格用“T”表示

输出格式

如果无法将框带到目标单元格,请打印-1

否则,在一行上输出两个整数p和W,其中p是推的次数,W是最优解中的行走次数

     HERE IS THE CODE.... my problem is  when input is:
    7 11
    ###########
    #T##......#
    #.#.#..####
    #....B....#
    #.######..#
    #.....S...#
    ###########
     output appears as -1. whereas it should hav been 6 22 
请告诉我代码中有什么错误

 #include <iostream>
    #include <queue>
    #include <string>
    #include <algorithm>
    using namespace std;
    const int MAX = 20 + 5; 
    char map[MAX][MAX]; 
    int visPerson[MAX][MAX];
    int visBox[MAX][MAX]; 
    int R, C;
    int dir[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
    char pushes[4] = {'E', 'W', 'S', 'N'}; 
    char walks[4] = {'e', 'w', 's', 'n'}; 
    string path; 
    struct NODE 
    {
     int br, bc; 
     int pr, pc; 
     string ans; 
    };
    int InMap(int r, int c) 
    {
     return (r >= 1 && r <= R && c >= 1 && c <= C);
    }
    int Bfs2(int sr, int sc, int er, int ec, int br, int bc, string &ans)
    {
     queue<NODE> q; 
     NODE node, tmpNode; 
     node.pr = sr; node.pc = sc; node.ans = "";
     q.push(node);
     visPerson[br][bc] = 1; 
     while (!q.empty()) 
     {
     node = q.front();
     q.pop();
     if (node.pr==er && node.pc==ec) { ans = node.ans; return 1; }
     if (visPerson[node.pr][node.pc]) continue;
     visPerson[node.pr][node.pc] = 1;
     for (int i=0; i<4; i++) 
     {
     int nr = node.pr + dir[i][0]; int nc = node.pc + dir[i][1];
     if (InMap(nr, nc) && !visPerson[nr][nc] && map[nr][nc] !='#')
     {
     tmpNode.pr = nr; tmpNode.pc = nc; tmpNode.ans = node.ans+ walks[i];
     q.push(tmpNode);
     }
     }
     }
     return 0;
    }
    int Bfs1(int sr, int sc, int br, int bc) 
    {
     queue<NODE> q; 
     NODE node, tmpNode; 
     node.pr = sr; node.pc = sc; node.br = br; node.bc = bc; node.ans ="";
     q.push(node);
     while (!q.empty()) 
     {
     node = q.front();
     q.pop();
     if (visBox[node.br][node.bc]) continue;
     visBox[node.br][node.bc] = 1;
     if (map[node.br][node.bc] == 'T') 
     {
     path = node.ans; return 1;
     }
     for (int i=0; i<4; i++) 
     {
     int nextr = node.br + dir[i][0];
     int nextc = node.bc + dir[i][1];
     int backR = node.br - dir[i][0];
     int backC = node.bc - dir[i][1];
     string ans = ""; 
     if (InMap(backR, backC) && InMap(nextr, nextc) && map[nextr][nextc] != '#'&& map[backR][backC] != '#' && !visBox[nextr][nextc])
     {
     if (Bfs2(node.pr, node.pc, backR, backC, node.br, node.bc, ans))
     {
     tmpNode.pr = node.br; tmpNode.pc = node.bc;
     tmpNode.br = nextr; tmpNode.bc = nextc;
     tmpNode.ans = node.ans + ans + pushes[i];
    q.push(tmpNode);
     }
     }
     }
     }
     return 0;
    }
    int main()
    {  int push=0,walk=0,len;
      string str;
        int i=1;
        int r=0;
     int sr, sc; 
     int br, bc; 
     int cases = 1; 
     cin>>R>>C;
     for (int r=1; r<=R; r++) 
     {
     for (int c=1; c<=C; c++)
     {
     cin >> map[r][c];
     if (map[r][c] == 'S'){ sr = r; sc = c; } 
     else if (map[r][c] == 'B') { br = r; bc = c; } 
     }
     }
     path = ""; 
     r= Bfs1(sr, sc, br, bc);
     if (r)
     {
        str=path;
        len= str.size();
        int a1= count(str.begin(),str.end(),'s');
        int a2= count(str.begin(),str.end(),'w');
        int a3= count(str.begin(),str.end(),'e');
        int a4= count(str.begin(),str.end(),'n');

        cout<< len - (a1+a2+a3+a4) << " " <<(a1+a2+a3+a4) << "\n";
     }
     else
     cout<< "-1"<<"\n";
      return 0;
    }
#包括
#包括
#包括
#包括
使用名称空间std;
常数int MAX=20+5;
字符映射[MAX][MAX];
整数visPerson[MAX][MAX];
int visBox[MAX][MAX];
int R,C;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0};
char push[4]={'E','W','S','N'};
char walks[4]={e',w',s',n'};
字符串路径;
结构节点
{
内特布尔,不列颠哥伦比亚省;
国际公共关系,个人电脑;
字符串ans;
};
int InMap(int r,int c)
{
返回(r>=1&&r=1&&c>c;
对于(int r=1;r映射[r][c];
如果(map[r][c]='S'){sr=r;sc=c;}
else如果(map[r][c]='B'){br=r;bc=c;}
}
}
路径=”;
r=Bfs1(sr、sc、br、bc);
if(r)
{
str=路径;
len=str.size();
int a1=计数(str.begin(),str.end(),'s');
int a2=计数(str.begin(),str.end(),'w');
int a3=计数(str.begin(),str.end(),'e');
int a4=计数(str.begin(),str.end(),'n');

这个解决方案可以处理任何输入吗?是的。@ScottHunter它可以处理这一个输入:8 4