-3
#include <iostream>
#include <vector>
using namespace std;
char img[30][15];
char x = 'X';
int dotLoc[24][2];
int dotNum = 0;
int ret[4];
int curDie = 0;
int dieNum = 0;
int der = 0;
vector<int>* readDice(vector<string>&);
int dieDots(int, int);
void findDots();
bool adjX(int,int);
bool traverse(int,int,int,int);
vector<int>* readDice(vector<string>& anImage) {
// TODO: implement method
vector<int> result;
//blank initialization
for (int poop = 0; poop < 4 ; poop ++){
ret[poop] = -1;
}
//blank initialization
for(int i = 0 ; i < 24 ; i++){
for(int j = 0; j < 2 ; j++){
dotLoc[i][j] = -1;
}
}
//blank initialization
for(int i = 0 ; i < 30 ; i++){
for(int j = 0; j < 15 ; j++){
img[i][j] = '.';
}
}
//blank initialization
dotNum = 0;
curDie = 0;
dieNum = 0;
der = 0;
// original to char array
for(int i = 0 ; i < anImage.size() ; i++){
for(int j = 0; j < anImage[i].size() ; j++){
img[i][j] = anImage.at(i)[j];
}
}
// initialize dotLoc to -1
for(int j = 0; j < 24 ; j++){
dotLoc[j][0] = -1;
dotLoc[j][1] = -1;
}
findDots();
for(int p = 0; p < 4 ; p++){
for(int j = 0; j < 24 ; j++){
if(!(dotLoc[j][0] == -1)){
der = j;
ret[p] = dieDots(dotLoc[j][0],dotLoc[j][1]);
}
}
}
for (int poop = 0; poop < 4 ; poop ++){
if (!(ret[poop] == -1)){
result.push_back(ret[poop]);
}
}
return &result;
}
int dieDots(int x, int y){
curDie = 0;
for(int j = 0; j < 24 ; j++){
if(!(dotLoc[j][0] == -1)){
if(traverse(x,y,dotLoc[j][0],dotLoc[j][1])){
curDie++;
dotLoc[j][0] = -1;
dotLoc[j][1] = -1;
}
}
}
dotLoc[der][0] = -1;
dotLoc[der][1] = -1;
return curDie;
}
void findDots(){
for(int i = 0; i <30 ; i++){
for(int j = 0; j <15 ; j++){
if(img[i][j] == x){
if (!adjX(i,j)){
dotLoc[dotNum][0] = i;
dotLoc[dotNum][1] = j;
dotNum++;
}
}
}
}
}
bool adjX(int a, int b){
if ((img[a-1][b]) == x || (img[a][b-1]) == x){
return true;
}
return false;
}
// True if same die
bool traverse(int x1, int y1, int x2, int y2){
if((x1 == x2) && (y1 == y2)){
return true;
}
if(img[x2][y2] == '.'){
return false;
}
int dX = x1-x2;
int dY = y1-y2;
bool lol;
if(dX > dY){
lol = traverse(x1, y1, x1+dX-1, y1+dY);
if (lol){
return true;
}
}
else{
lol = traverse(x1, y1, x1+dX, y1+dY-1);
if (lol){
return true;
}
}
return false;
}
int main (int argc, char * const argv[]) {
cout << "testing C++ readDice method..." << endl;
vector<string> input;
vector<int> expected;
for (int i = 0; i < 10; i++) {
input .clear();
expected.clear();
switch (i) {
case 0:
input .push_back("***...***.***..");
input .push_back("*X*..*X*...*X*.");
input .push_back("***.***.....***");
expected.push_back(1);
expected.push_back(1);
expected.push_back(1);
break;
case 1:
input .push_back("****..****");
input .push_back("*X**..XX**");
input .push_back("**X*..**XX");
input .push_back("****..****");
expected.push_back(2);
expected.push_back(2);
break;
case 2:
input .push_back(".***..X**..X*X.");
input .push_back(".*X*..*X*..*X*.");
input .push_back(".***..**X..X*X.");
expected.push_back(1);
expected.push_back(3);
expected.push_back(5);
break;
case 3:
input .push_back("*X*X*.*****......");
input .push_back("*****.*X*X*.**X**");
input .push_back("*X*X*.**X**.*X*X*");
input .push_back("*****.*X*X*.**X**");
input .push_back("*X*X*.*****......");
expected.push_back(4);
expected.push_back(5);
expected.push_back(6);
break;
case 4:
input .push_back("..............................");
input .push_back("..............................");
input .push_back("...............*..............");
input .push_back("...*****......****............");
input .push_back("...*X***.....**X***...........");
input .push_back("...*****....***X**............");
input .push_back("...***X*.....****.............");
input .push_back("...*****.......*..............");
input .push_back("..............................");
input .push_back("........***........******.....");
input .push_back(".......**X****.....*X**X*.....");
input .push_back("......*******......******.....");
input .push_back(".....****X**.......*X**X*.....");
input .push_back("........***........******.....");
input .push_back("..............................");
expected.push_back(1);
expected.push_back(2);
expected.push_back(2);
expected.push_back(4);
break;
case 5:
input .push_back(".................******...");
input .push_back("....*.............**X***..");
input .push_back("...*X*.............***X**.");
input .push_back(".******.............******");
input .push_back("*X*XX*X*..................");
input .push_back(".******.*****.......******");
input .push_back("..*X*...***X*......**XX**.");
input .push_back("...*....**X**.....**XX**..");
input .push_back("........*X***....******...");
input .push_back("........*****.............");
expected.push_back(1);
expected.push_back(2);
expected.push_back(3);
expected.push_back(5);
break;
case 6:
input .push_back("......*.............");
input .push_back(".....*X**...........");
input .push_back("....****X*..........");
input .push_back("...*X****.....******");
input .push_back("....**X*.....**X***.");
input .push_back("......*.....***X**..");
input .push_back("...........******...");
input .push_back("........***.........");
input .push_back(".......****X**......");
input .push_back("......**XX***.......");
input .push_back(".....*X*****........");
input .push_back("........***.........");
expected.push_back(1);
expected.push_back(3);
expected.push_back(4);
break;
case 7:
input .push_back("............................");
input .push_back(".......**...................");
input .push_back("......**X**.........**......");
input .push_back(".....*X***X*......*X***.....");
input .push_back("......**X**......***X***....");
input .push_back("........**...**...***X*.....");
input .push_back("...**.......*X*X*...**......");
input .push_back(".**X**.....***X***..........");
input .push_back("***X***.....*X*X*...........");
input .push_back(".**X**........**............");
input .push_back("..**........................");
input .push_back("............................");
expected.push_back(1);
expected.push_back(3);
expected.push_back(4);
expected.push_back(5);
break;
case 8:
input .push_back("......*.............");
input .push_back(".....*X**...........");
input .push_back("....*X**X*..........");
input .push_back("...*X**X*.....******");
input .push_back("....**X*.....X*X*X*.");
input .push_back("......*.....X*X*X*..");
input .push_back("...........******...");
input .push_back("....***.............");
input .push_back("...**X*X**.**X*X**..");
input .push_back("..**X*X**...**X*X**.");
input .push_back(".*X**X**.....**X**X*");
input .push_back("....***.............");
expected.push_back(6);
expected.push_back(6);
expected.push_back(6);
expected.push_back(6);
break;
case 9:
input .push_back("..........*****");
input .push_back("..**......**X**");
input .push_back(".*XX*.....*****");
input .push_back("*****..........");
input .push_back("*XX*...X**.....");
input .push_back(".**...**X**....");
input .push_back(".....XX**XX*...");
input .push_back("......*X***....");
input .push_back(".......*X*.....");
expected.push_back(1);
expected.push_back(2);
expected.push_back(6);
break;
default:
cout << "should not reach here!" << endl;
break;
}
vector<int>* actual = readDice(input);
bool ok = expected == *actual;
cout << "test " << i << ": " << (ok ? "ok" : "failed") << endl;
delete actual;
}
cout << "done!\n";
return 0;
}
它給了我一個運行時錯誤輸入「:0000005:未處理的異常在0x5b9fad4a(msvcp100d.dll)在painis.exe訪問衝突讀取位置0xccccccd0。」C++閱讀從骰子訪問衝突
此外,它創建了一個名爲xutility另一個文件,在這一點上 C++語法(切換純文本)
for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)
(*_Pnext)->_Myproxy = 0;
_Myproxy->_Myfirstiter = 0;
}
歡迎來到StackOverflow。請在問題中多加一點努力,並查看[FAQ](http://stackoverflow.com/faq)。這將表明你已經嘗試過自己。您發佈的內容無助於確定您實際要求的內容。 – bitmask 2012-02-15 01:12:21