BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #90585同步于 2016/3/16
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

奇了个怪了,大神帮看看

ddark
2016/3/16镜像同步19 回复
struct state { char value; int x; int y; int next; state(char val, int xx, int yy, int nex) :value(val), x(xx), y(yy), next(nex) {}; }; bool exist(vector<vector<char>>& board, string word) { int row = board.size(); int range = board[0].size(); stack<state> sstack; for (int i = 0; i < row; i++) { for (int j = 0; j < range; j++) { if (board[i][j] == word[0]) { state first(word[0], i,j,0); sstack.push(first); board[sstack.top().x][sstack.top().y] = '*'; 调试里看到 sstack[0]里 y 和 next 都是-842150451???我知道我很菜[ema1][ema1][ema1] 参数word是"SEE",断点处i=1,j=0,在sstack.push(first);执行后 出现了上述情况.。。
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
chenxiansf机器人#1 · 2016/3/16
因为word,i,j还没赋值就在初始化first时用了,所以瞬间爆炸
chenxiansf机器人#2 · 2016/3/16
其实我是瞎猜的,我也不知道怎么回事
FromMars机器人#3 · 2016/3/16
卤煮要不要把下面这两句调换一下顺序试试? state first(word[0], i,j,0); word="SEE",i=1,j=0;
sonyok17机器人#4 · 2016/3/16
Effective C++ Item04: 对象使用之前先初始化,不要依赖编译器去完成
ddark机器人#5 · 2016/3/16
【 在 chenxiansf 的大作中提到: 】 : 因为word,i,j还没赋值就在初始化first时用了,所以瞬间爆炸 刚刚情况没说清楚,以改正,请阅[ema11]
ddark机器人#6 · 2016/3/16
【 在 FromMars 的大作中提到: 】 : 卤煮要不要把下面这两句调换一下顺序试试? : state first(word[0], i,j,0); : word="SEE",i=1,j=0; 哎下面这句是为了说明情况,其实是函数参数word="SEE",题目已改,请阅
November30机器人#7 · 2016/3/16
lz要不要把代码都贴上来看看?
ddark机器人#8 · 2016/3/16
【 在 November30 的大作中提到: 】 : lz要不要把代码都贴上来看看? leetcode上的题目,写的很丑陋我知道[ema23],您瞧瞧。 #include<vector> #include<string> #include<stack> #include<iostream> using namespace std; struct state { char value; int x; int y; int next; state(char val, int xx, int yy, int nex) :value(val), x(xx), y(yy), next(nex) {}; }; class Solution79 { public: bool exist(vector<vector<char>>& board, string word) { int row = board.size(); int range = board[0].size(); stack<state> sstack; for (int i = 0; i < row; i++) { for (int j = 0; j < range; j++) { if (board[i][j] == word[0]) { state first(word[0], i,j,0); sstack.push(first); board[sstack.top().x][sstack.top().y] = '*'; int wordIndex = 0; while (!sstack.empty()) { if (sstack.top().next == 0) { if ((sstack.top().x - 1) < 0) { sstack.top().next = 1; continue; } if (board[sstack.top().x - 1][sstack.top().y] == word[wordIndex + 1]) { struct state top(sstack.top().x - 1, sstack.top().y, word[wordIndex + 1], 0); sstack.push(top); board[sstack.top().x - 1][sstack.top().y] = '*'; wordIndex++; if (wordIndex == word.size()) { return true; } } else { sstack.top().next = 1; continue; } } if (sstack.top().next == 1) { if ((sstack.top().y +1) >= range) { sstack.top().next = 2; continue; } if (board[sstack.top().x ][sstack.top().y+1] == word[wordIndex + 1]) { struct state top(sstack.top().x , sstack.top().y+1, word[wordIndex + 1], 0); sstack.push(top); board[sstack.top().x ][sstack.top().y+1] = '*'; wordIndex++; if (wordIndex == word.size()) { return true; } } else { sstack.top().next = 2; continue; } } if (sstack.top().next == 2) { if ((sstack.top().x + 1) >=row) { sstack.top().next = 3; continue; } if (board[sstack.top().x+1][sstack.top().y ] == word[wordIndex + 1]) { struct state top(sstack.top().x+1, sstack.top().y , word[wordIndex + 1], 0); sstack.push(top); board[sstack.top().x + 1][sstack.top().y] = '*'; wordIndex++; if (wordIndex == word.size()) { return true; } } else { sstack.top().next = 3; continue; } } if (sstack.top().next == 3) { if ((sstack.top().x - 1) < 0) { sstack.top().next = 4; continue; } if (board[sstack.top().x - 1][sstack.top().y] == word[wordIndex + 1]) { struct state top(sstack.top().x - 1, sstack.top().y, word[wordIndex + 1], 0); sstack.push(top); board[sstack.top().x - 1][sstack.top().y] = '*'; wordIndex++; if (wordIndex == word.size()) { return true; } } else { sstack.top().next = 4; continue; } } if (sstack.top().next == 4) { wordIndex--; board[sstack.top().x][sstack.top().y] = sstack.top().value; sstack.pop(); } } return false; } } } } }; int main() { Solution79 s = Solution79(); vector<char> t{ 'A','B','C','E' }; vector<vector<char>> ts; ts.push_back(t); t = { 'S','F','C','S' }; ts.push_back(t); t = { 'A','D','E','E' }; ts.push_back(t); cout<<s.exist(ts, "SEE"); cin.get(); return 0; }
ykprocess机器人#9 · 2016/3/16
太长了 lz可以试试把重复粘贴的代码复用一下,也许分析起来会方便很多