virtual

Virtual Judge POJ 2251 Dungeon Master

倾然丶 夕夏残阳落幕 提交于 2019-12-02 13:14:26
三维数组加宽搜 #include <stdlib.h> #include <string.h> #include <stdio.h> const int MAXN=50; int c, k, h; char ma[MAXN][MAXN][MAXN]; //定义三维数组 长宽高 int visit[MAXN][MAXN][MAXN]; //标记数组 struct node { int c, k, h;//结构体记录到达某个点 c长k宽h高 int step;//走的步数 }; struct node t[33433];//结构体队列 struct node p, q, w, l; int f[][3] = {0,0,1, 0,0,-1, 0,1,0, 0,-1,0, 1,0,0, -1,0,0};//向上下左右前后六个方向移动 包括上楼 void bfs() { visit[p.c][p.k][p.h] = 1;//标记已经走过 int front = 0, rear = 0; t[rear++] = p;//入队 while(front!=rear) { //当队列不为空的时候 w = t[front++]; if(w.c==q.c&&w.k==q.k&&w.h==q.h) { //如果是终点,直接输出 printf("Escaped in %d minute(s).\n", w

Virtual Judge POJ 3278 Catch That Cow

情到浓时终转凉″ 提交于 2019-12-02 13:14:20
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<queue> const int maxn =100010; using namespace std; int n,m; struct data { int x,step; } p; int vis[100005]; void bfs() { memset(vis,0,sizeof(vis)); //重置 queue<data>q; //队列 p.x=n; //起点 p.step=0; //起点到起点的距离 q.push(p); //压入队列 vis[n]=1; //走过 while(!q.empty()) { p=q.front(); q.pop(); if(p.x==m) { printf("%d\n",p.step); //直接输出 return ; } data k=p; if(k.x+1<maxn&&!vis[k.x+1]) { //尝试前进一步 k.x++; //如果可以,加坐标 vis[k.x]=1; //标记走过 k.step++; // 加步数 q.push(k); //新的位置压入队列 } k=p; if(k.x-1>=0&&!vis[k.x-1]) { /

Virtual Judge POJ 1328 Radar Installation

痞子三分冷 提交于 2019-12-02 13:14:18
贪心 #include<algorithm> #include<iostream> #include<cstdio> #include<cmath> using namespace std; struct Radar { double start,end; } radar[1005]; bool cmp(Radar a,Radar b) { return a.start<b.start; } int main() { int n,d,x,y,m,num,flag; double l,r; m = 1; while(scanf("%d%d",&n,&d)) { if(!n && !d) //如果为0 break; flag = true; for(int i = 0; i < n; i++) { scanf("%d%d",&x,&y); if(y > d) flag = false; radar[i].start = x - sqrt(d * d - y * y); //勾股定理 radar[i].end = x + sqrt(d * d - y * y); //区间覆盖范围 } if(!flag) { printf("Case %d: -1\n",m++); continue; } sort(radar,radar + n,cmp); //排序 n为岛屿数目 num = 1,l =

重载与多态

…衆ロ難τιáo~ 提交于 2019-12-02 13:07:53
多态的类型 :分为4类,重载多态,强制多态,包含多态,参数多态。 以前所学过的普通函数的重载也属于重载多态。强制多态是指将一个变元的类型加以变化,以符合一个函数或操作的要求,比如int型与float型相加,要先进行类型转换。 多态的实现 :分为两类,编译时的多态与运行时的多态。 前者在编译的过程中确定了同名的具体操作对象,而后者是在程序运行过程中才多态地确定操作所指向的对象。这种确定操作具体对象的过程就是绑定。绑定工作在编译连接阶段完成的情况为 静态绑定 ,在程序运行过程中完成的情况是 动态绑定 。 运算符重载 运算符重载时对已有的运算符赋予多重含义,使同一个运算符作用于不同的数据类型时有不同的行为。 重载形式 重载为类的非静态成员函数,重载为非成员函数。 两种重载方式的声明语法形式一般相同 都为 返回类型 operator 运算符(形参表) { 函数体 } 非成员函数一般定义为友元函数。 **实现‘+’的重载** #include<iostream> using namespace std; class counter { private: float a; public: counter(float a=0):a(a){} counter operator+(counter& c)const { **//定义为类的非静态成员函数** return counter(a + c.a

多态性总结

青春壹個敷衍的年華 提交于 2019-12-02 12:47:41
  多态从实现的角度可以划分为:编译时多态和运行时的多态。 运算符重载   运算符重载即静态多态,是对已有的运算符赋予多重含义,运算符重载是通过创建运算符函数实现的,运算符函数定义了重载的运算符将要进行的操作。运算符函数的定义与其他函数的定义类似,唯一的区别是运算符函数的函数名是由关键字 operator 和其后要重载的运算符符号构成的。把指定的运算表达式转化为对运算符函数的调用,运用对象转化为运算符函数的实参。 • 运算符重载格式 函数类型 operator运算符 (形参){ …… } • 运算符重载规则 当重载为类的成员函数的情况,形式参数个数=原操作数个数-1(后置++、--除外)。 当重载为类友元函数的情况,形式参数个数=原操作数个数。 除了类属关系运算符 "." 、成员指针运算符 ".*" 、作用域运算符 "::" 、sizeof 运算符和三目运算符 "?:" 以外,C++ 中的所有运算符都可以重载。 重载运算符限制在 C++ 语言中已有的运算符范围内的允许重载的运算符之中,不能创建新的运算符。 //复数类成员函数自增 #include<iostream> using namespace std; class complex{ private: int real,imag; public: complex(int r,int i){ real=r; imag=i; }

C++多态小结

眉间皱痕 提交于 2019-12-02 12:34:52
C++ 多态 多态 多态 按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。 C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。 多态与非多态的实质区别就是函数地址是早绑定还是晚绑定 。如果函数的调用,在编译器编译期间就可以确定函数的调用地址,并生产代码,是静态的,就是说地址是早绑定的。而如果函数调用的地址不能在编译器期间确定,需要在运行时才确定,这就属于晚绑定。 下面的实例中,基类 Shape 被派生为两个类,如下所示: #include <iostream> using namespace std; class Shape { protected: int width, height; public: Shape( int a=0, int b=0){ width = a; height = b; } int area(){ cout << "Parent class area :" <<endl; return 0; } }; class Rectangle: public Shape{ public: Rectangle( int a=0, int b=0):Shape(a, b) {} int area (){ cout << "Rectangle class area :" <<endl; return

Can I tell if a C++ virtual function is implemented

人走茶凉 提交于 2019-12-02 12:29:51
问题 I want to be able to tell at run-time if an instance of a class implements a virtual function. For example: struct Base { virtual void func(int x) { <default behavior here> } virtual void func2(int x) { <default behavior here> } }; struct Deriv : Base { virtual void func(int x) override { <new behavior here> } }; int main() { Base *d = new Deriv; if(implements<Base::func(int)>(d)) // This should evaluate true {} if(implements<Base::func2(int)>(d)) // This should evaluate false {} } I have

C++ virtual function from constructor [duplicate]

耗尽温柔 提交于 2019-12-02 11:57:07
This question already has an answer here: Calling virtual functions inside constructors 13 answers Why the following example prints "0" and what must change for it to print "1" as I expected ? #include <iostream> struct base { virtual const int value() const { return 0; } base() { std::cout << value() << std::endl; } virtual ~base() {} }; struct derived : public base { virtual const int value() const { return 1; } }; int main(void) { derived example; } Because base is constructed first and hasn't "matured" into a derived yet. It can't call methods on an object when it can't guarantee that the

Force template method in non-template class

纵然是瞬间 提交于 2019-12-02 11:48:49
问题 I try to achieve the following behavior/syntax/usage of this class: Data1 dataType1; Data2 dataType2; int intType; float floatType; dataType1.method( intType ); dataType1.method( floatType ); dataType2.method( intType ); dataType2.method( floatType ); My approach would be this: struct CDataBase { template< typename T > virtual void method( T type ) = 0; }; struct CData1 : CDataBase { template< typename T > void method( T type ) {} }; struct CData2 : CDataBase { template< typename T > void

详谈C++中的多态

和自甴很熟 提交于 2019-12-02 11:32:06
详谈C++中的多态 1.多态基本概念 多态是面向对象程序设计语言中数据抽象和继承之外的第三个基本特征。多态性(polymorphism)提供接口与具体实现之间的另一层隔离,从而将”what”和”how”分离开来。 c++支持编译时多态(静态多态)和运行时多态(动态多态),运算符重载和函数重载就是编译时多态,而派生类和虚函数实现运行时多态。 //计算器 class Caculator{ public: void setA(int a){ this->mA = a; } void setB(int b){ this->mB = b; } void setOperator(string oper){ this->mOperator = oper; } int getResult(){ if (this->mOperator == "+"){ return mA + mB; } else if (this->mOperator == "-"){ return mA - mB; } else if (this->mOperator == "*"){ return mA * mB; } else if (this->mOperator == "/"){ return mA / mB; } } private: int mA; int mB; string mOperator; };