C++ ssd5 16 optional exercise 3

#ifndef GRID_H
#define GRID_H

#include <string>
#include <vector>

using namespace std;

/*
* IMPORTANT NOTE:
*
* For this assignment, you might need to add state to the
* class and/or argment existing methods, and/or create private helper
* methods, but you should not delare new public methods
*/

const bool INFECTED = true;
const bool NOT_INFECTED = false;

class grid;

class grid {

private:
int rows;
int cols;
int num;
vector<bool> *area;
vector<bool> *mark;

int indexof(int row, int col) const;
bool infected(int row, int col) const;

public:

struct offsets
{
int x, y;
char* dir;

};
offsets pace[8] = { { -1,0,"N" },{ -1,1,"NE" },{ 0,1,"E" },{ 1,1,"SE" },{ 1,0,"S" },{ 1,-1,"SW" },{ 0,-1,"W" },{ -1,-1,"NW" } };
offsets downpace[5] = { {0,1},{1,1},{1,0},{1,-1},{0,-1} };
offsets uppace[5] = { {-1,0},{-1,1},{0,1},{0,-1},{-1,-1} };
offsets rightpace[5] = { {-1,0},{-1,1},{0,1},{1,1},{1,0} };
offsets leftpace[5] = { {-1,0},{1,0},{1,-1},{0,-1},{-1,-1} };
offsets nwpace[3] = { {0,1},{1,1},{1,0} };

grid(string file);
~grid();

int count(int row, int col);

friend ostream &operator<<(ostream &stream, const grid& ob);

};

#endif
#pragma once

#include <iostream>
#include <fstream>

using namespace std;

#include "grid.h"

// You do not need to alter function indexof.
int grid::indexof(int row, int col) const {
return row*cols + col;
}

// You do not need to alter function infected.
bool grid::infected(int row, int col) const {
return (area->operator[](indexof(row, col)) == INFECTED);
}

// You may need to alter the constructor
grid::grid(string file) {

ifstream grid_file;

grid_file.open(file.c_str());

if (!grid_file)
{
cerr << "can not open this file" << endl;
return;
}

grid_file >> rows;
grid_file >> cols;

area = new vector<bool>(rows*cols, NOT_INFECTED);
mark = new vector<bool>(rows*cols, INFECTED);

while (true) {

int blob_row;
int blob_col;

grid_file >> blob_row;
grid_file >> blob_col;

if (grid_file.eof()) {
break;
}

area->operator[](indexof(blob_row, blob_col)) = INFECTED;
}

grid_file.close();

num = 0;
}

// You may need to alter the destructor
grid::~grid() {
delete area;
delete mark;
}

// You will need to alter this function to display the
// plus signs (+) next to the cells that belong to
// a counted colony.
ostream &operator<<(ostream &stream, const grid& ob) {

for (int row = 0; row < ob.rows; row++) {

for (int col = 0; col < ob.cols; col++) {
if (ob.mark->operator[](ob.indexof(row, col)) == NOT_INFECTED)
{
stream << ob.area->operator[](ob.indexof(row, col))<<"+";
stream << " ";
}
else
{
stream << ob.area->operator[](ob.indexof(row, col));
stream << " ";
}
}

stream << endl;
}

stream << endl;
return stream;
}

// Replace the return statement in this function with your
// recursive implementation of this method */
int grid::count(int row, int col) {

if (row == 0 && col == 0)//右上角的顶点,其他三个顶点类似,就不再写了。。。。
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;
num++;
for (int i = 0; i < 3; i++)
{
row += nwpace[i].x;
col += nwpace[i].y;

if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{

count(row, col);
}
else
{
row = row - nwpace[i].x;
col = col - nwpace[i].y;
}
}
}

if (row == 0 && col != 0)//到达第一行时
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;
num++;
for (int i = 0; i < 5; i++)
{
row += downpace[i].x;
col += downpace[i].y;

if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{

count(row, col);
}
else
{
row = row - downpace[i].x;
col = col - downpace[i].y;
}
}
}
if (row == 5)//到达最后一行时
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;
num++;
for (int i = 0; i < 5; i++)
{
row += uppace[i].x;
col += uppace[i].y;

if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{

count(row, col);
}
else
{
row = row - uppace[i].x;
col = col - uppace[i].y;
}
}
}

if (col == 0 && row != 0)
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;
num++;
for (int i = 0; i < 5; i++)
{
row += rightpace[i].x;
col += rightpace[i].y;

if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{

count(row, col);
}
else
{
row = row - rightpace[i].x;
col = col - rightpace[i].y;
}
}
}

if (col == 5)
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;
num++;
for (int i = 0; i < 5; i++)
{
row += leftpace[i].x;
col += leftpace[i].y;

if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{

count(row, col);
}
else
{
row = row - leftpace[i].x;
col = col - leftpace[i].y;
}
}
}

if (row != 0 && row != 5 && col != 0 && col != 5)//中间的部分
{
if (area->operator[](indexof(row, col)) == true)//如果改点被感染了
{
mark->operator[](indexof(row, col)) = NOT_INFECTED;//标记
num++;
for (int i = 0; i < 8; i++)//从八个方向去试探
{
row += pace[i].x;
col += pace[i].y;

if (area->operator[](indexof(row, col)) == INFECTED && mark->operator[](indexof(row, col)) == INFECTED)
{

count(row, col);//递归
}
else//如果改点行不通就回溯
{
row = row - pace[i].x;
col = col - pace[i].y;
}

}
}
}

return num;
}

时间: 2024-12-25 18:38:35

C++ ssd5 16 optional exercise 3的相关文章

C++ ssd5 25 optional exercise 6

// parts.h #ifndef _PARTS_H_#define _PARTS_H_ #include <vector>#include <map>#include <string>#include <iostream> using namespace std; //**************** Part ****************class Part { private: static int count; public: string n

C++ ssd5 27 optional exercise 7

#include "RailSystem.h" void RailSystem::reset(void) { // TODO: reset the data objects of the // City objects' contained in cities map<string, City*>::iterator iter; for (iter = cities.begin(); iter != cities.end(); iter++) { iter->seco

C++ ssd5 12 optional exercise2

#pragma once#ifndef FIFO_H#define FIFO_H#include"simulator.h" class fifo : public simulator{public: fifo(int second_per_page); virtual void simulate(string file); //virtual void addevent(event e); private: //queue<event> wait;}; #endif #in

Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 2)及总结

Exercise 1:Linear Regression---实现一个线性回归 关于如何实现一个线性回归,请参考:http://www.cnblogs.com/hapjin/p/6079012.html Exercise 2:Logistic Regression---实现一个逻辑回归 问题描述:用逻辑回归根据学生的考试成绩来判断该学生是否可以入学. 这里的训练数据(training instance)是学生的两次考试成绩,以及TA是否能够入学的决定(y=0表示成绩不合格,不予录取:y=1表示录

斯坦福大学机器学习公开课:Programming Exercise 2: Logistic Regression

斯坦福大学机器学习公开课:Programming Exercise 2: Logistic Regression---Matlab实现 1 Logistic Regression In this part of the exercise, I will build a logistic regression model to predict whether a student gets admitted into a university. You want to determine each

java1.8新特性(optional 使用)

经常在程序中出现 java.lang.NullPointerException  为了避免  报错,总是要进行一些 是否为null 的if else 判断 ,1.8 可以使用optional 类 来简化处置   optional :A container object which may or may not contain a non-null value.:可能包含也可能不包含非空值的容器对象. 既然optional 是一个容器对象,那就应该先创建该 对象 才能调用该对象的一些方法 创建op

Java8新特性 - Optional容器类

Optional 类(java.util.Optional) 是一个容器类,代表一个值存在或不存在,原来用null 表示一个值不存在,现在Optional 可以更好的表达这个概念.并且可以避免空指针异常. Optional.of(T t) : 创建一个Optional 实例 Optional.empty() : 创建一个空的Optional 实例 Optional.ofNullable(T t):若t 不为null,创建Optional 实例,否则创建空实例 isPresent() : 判断是否

coursera 机器学习 logistic regression 逻辑回归的项目

github : https://github.com/twomeng/logistic-regression- ex1. m 1 %% Machine Learning Online Class - Exercise 2: Logistic Regression 2 % 3 % Instructions 4 % ------------ 5 % 6 % This file contains code that helps you get started on the logistic 7 %

IOS以无线方式安装企业内部应用(开发者)

请先阅读:http://help.apple.com/deployment/ios/#/apda0e3426d7 操作系统:osx yosemite 10.10.5 (14F1509) xcode:Version 7.2 (7C68) 1.Product-- >Archive,如果成功则会弹出Organizer--Archives界面,选择Export 2.选择“企业部署方式” 3.选择相应的账号 4.选择“所有设备” 5.勾选“生成plist文件” 通过无线方式安装需要用到plist文件,所以